Version 21.12 will be released Sunday (2021-12-26). I am working thru finalizing the docs right now. The features I was specifically thinking of are from PR #2304.
task1 = app.add_task(dummy, name="dummy_task")
task2 = app.get_task("dummy_task")
assert task1 is task2
app.cancel_task("dummy_task")
for task in app.tasks:
print(task.is_cancelled())
app.purge_tasks()
To answer the question at hand, see below.
async def background():
try:
print("Sleeping in the background")
await asyncio.sleep(20)
finally:
print("[FINALLY] background")
@app.websocket("/q")
async def handler(request, ws):
request.app.add_task(background())
try:
while True:
print(".")
await asyncio.sleep(1)
finally:
print("[FINALLY] handler")
@app.before_server_stop
async def cleanup(app, _):
for task in asyncio.all_tasks():
if task.get_coro().__name__ == "background":
task.cancel()
What you want to do is call cancel
on the task. Unfortunately, in v21.9 there is no good way to get access to that task to be able to call cancel on it (this was the reason it is being added to v21.12). Therefore, you have to loop thru the all_tasks
to find the one that you want. Not ideal.