reaction
reaction copied to clipboard
Save a reference to each task created...
A comment appeared in the Python 3.9+ asyncio documentation :
async def coro():
...
# In Python 3.7+
task = asyncio.create_task(coro())
"Important : Save a reference to the result of this function, to avoid a task disappearing mid execution."
The issue is that if the assignment task= isn't made, then (it seems) Python 3.9+ will automatically clean up the unused reference - ending the async task (which will complain about being terminated, etc), i.e. the following will ~fail :
async def coro():
...
# In Python 3.9+
asyncio.create_task(coro())
If someone is listening : I'd be happy to do a PR to fix this issue in the examples and README. It was really quite puzzling to debug since the apparent error and what creates it (Python internals being changed, and a small note in the docs) are pretty distinct.