Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save a reference to each task created... #12

Open
mdda opened this issue Feb 21, 2022 · 0 comments
Open

Save a reference to each task created... #12

mdda opened this issue Feb 21, 2022 · 0 comments

Comments

@mdda
Copy link

mdda commented Feb 21, 2022

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 mdda changed the title Save a reference each task created... Save a reference to each task created... Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant