import asyncio from contextlib import asynccontextmanager import uvicorn from fastapi import FastAPI from config import MYSQL_HOST, MYSQL_PORT, MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASS from internal_loop import internal_loop, internal_loop_stop from api import testAPI from influx_related import init_influx from mysql_related import mysql_init, get_mysql_session_local, get_mysql_engine @asynccontextmanager async def startup(fast_api: FastAPI): # Startup logic print("startup") influxdb_client = init_influx(fast_api) mysql_init('mysql', MYSQL_HOST, MYSQL_PORT, MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASS ) mysql_client = get_mysql_session_local() mysql_engine = get_mysql_engine() print(f"{mysql_engine=}") print("mysql init done") task = asyncio.create_task(internal_loop(influxdb_client, mysql_client)) try: yield finally: # Shutdown logic await internal_loop_stop(task) await influxdb_client.close() mysql_engine = get_mysql_engine() await mysql_client.close() await mysql_engine.dispose() app = FastAPI(lifespan=startup) app.include_router( testAPI.router, prefix='/api', tags=['testApi'], ) # Press the green button in the gutter to run the script. if __name__ == '__main__': uvicorn.run( "main:app", host='0.0.0.0', port=8082, log_level='debug', )