Make sure to keep a reference to asyncio tasks
as suggested in the docs
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user