Make sure to keep a reference to asyncio tasks

as suggested in the docs
This commit is contained in:
Christoph Reiter
2023-04-22 20:33:31 +02:00
parent 95fb5e7cb4
commit 96c1dc7365
2 changed files with 11 additions and 2 deletions

View File

@@ -21,11 +21,16 @@ if not os.environ.get("NO_MIDDLEWARE"):
app.middleware("http")(check_is_ready)
_background_tasks = set()
# https://github.com/tiangolo/fastapi/issues/1480
@app.on_event("startup")
async def startup_event() -> None:
if not os.environ.get("NO_UPDATE_THREAD"):
asyncio.create_task(update_loop())
task = asyncio.create_task(update_loop())
_background_tasks.add(task)
task.add_done_callback(_background_tasks.discard)
@webapp.exception_handler(Exception)

View File

@@ -435,9 +435,13 @@ async def trigger_loop() -> None:
await asyncio.sleep(UPDATE_INTERVAL)
queue_update()
_background_tasks = set()
async def update_loop() -> None:
asyncio.create_task(trigger_loop())
task = asyncio.create_task(trigger_loop())
_background_tasks.add(task)
task.add_done_callback(_background_tasks.discard)
while True:
async with _rate_limit:
print("check for updates")