bg task cancelling

This commit is contained in:
marios8543 2025-04-26 20:32:31 +03:00
parent b2800303fe
commit 44a70d3791

View File

@ -9,6 +9,7 @@ from uvicorn import run
app = FastAPI() app = FastAPI()
event = Event() event = Event()
background_tasks = []
tunerlist = TunerList() tunerlist = TunerList()
bluetooth = Bluetooth() bluetooth = Bluetooth()
@ -43,7 +44,12 @@ async def startup_event():
tunerlist_listener(), tunerlist_listener(),
bluetooth_listener() bluetooth_listener()
]: ]:
create_task(i) background_tasks.append(create_task(i))
@app.on_event("shutdown")
async def shutdown_event():
for task in background_tasks:
task.cancel()
@app.websocket("/ws") @app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket): async def websocket_endpoint(websocket: WebSocket):