Correct stop of loop, mysql and influx

This commit is contained in:
marys
2025-09-26 11:34:12 +02:00
parent c850a07ff0
commit 521c474a17
3 changed files with 43 additions and 20 deletions

32
main.py
View File

@@ -2,14 +2,14 @@ import asyncio
from contextlib import asynccontextmanager
import uvicorn
from fastapi import (FastAPI, Depends)
from fastapi import FastAPI
from config import MYSQL_HOST, MYSQL_PORT, MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASS
from internal_loop import internal_loop
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_db, get_session_local
from mysql_related import mysql_init, get_mysql_session_local, get_mysql_engine
@asynccontextmanager
@@ -24,14 +24,26 @@ async def startup(fast_api: FastAPI):
MYSQL_USER,
MYSQL_PASS
)
mysql_client = get_session_local()
mysql_client = get_mysql_session_local()
mysql_engine = get_mysql_engine()
print(f"{mysql_engine=}")
print("mysql init done")
asyncio.create_task(internal_loop(influxdb_client, mysql_client))
yield
# Shutdown logic
await influxdb_client.close()
#await mysql_engine.dispose()
task = asyncio.create_task(internal_loop(influxdb_client, mysql_client))
try:
yield
finally:
# Shutdown logic
internal_loop_stop()
try:
await task
print("Cancelled")
except asyncio.CancelledError:
print("Cancelled exception")
pass
await influxdb_client.close()
mysql_engine = get_mysql_engine()
await mysql_client.close()
await mysql_engine.dispose()
app = FastAPI(lifespan=startup)