reaction icon indicating copy to clipboard operation
reaction copied to clipboard

Save a reference to each task created...

Open mdda opened this issue 3 years ago • 0 comments

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.

mdda avatar Feb 21 '22 06:02 mdda