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

Confusing asyncio.create_task documentation about task lifetime #117379

Closed
sklibanov312 opened this issue Mar 29, 2024 · 1 comment
Closed

Confusing asyncio.create_task documentation about task lifetime #117379

sklibanov312 opened this issue Mar 29, 2024 · 1 comment
Labels
docs Documentation in the Doc dir topic-asyncio

Comments

@sklibanov312
Copy link

Documentation

The document for asyncio.create_task - https://docs.python.org/3.10/library/asyncio-task.html#asyncio.create_task - states very plainly:

Important Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done. For reliable “fire-and-forget” background tasks, gather them in a collection:

Is there a program that proves this behavior to be true? Here's my attempt at showing this behavior:

import asyncio
import gc

async def forever():
    while True:
        print('forever')
        await asyncio.sleep(1)
		
async def forever_and_one_day():
    # From https://docs.python.org/3.10/library/asyncio-task.html#asyncio.create_task :
    # "Save a reference to the result of this function, to avoid a task disappearing mid-execution."
    # So ... shouldn't it disappear? But it doesn't? When will this hurt?
    asyncio.create_task(forever())
    while True:
        print('and a day')
        await asyncio.sleep(1)
        gc.collect()
        

asyncio.run(forever_and_one_day())

However, I ran this example in Python 3.9, 3.10, 3.11 and 3.12 and it continues printing 'forever' and 'and a day' for as long as I care to let it run. According to my interpretation of the docs, it ought to stop printing 'forever', because the result of create_task is not saved anywhere. But it doesn't stop, it just continues.

So what does this documentation actually mean? How do we change this program to exhibit the documented behavior of tasks disappearing mid-execution?

@sklibanov312 sklibanov312 added the docs Documentation in the Doc dir label Mar 29, 2024
@github-project-automation github-project-automation bot moved this to Todo in asyncio Mar 29, 2024
@gvanrossum
Copy link
Member

There are definitely cases where this can happen (we've had to create workarounds in asyncio code itself).

I don't have a quick example, but I recommend you follow gh-91887, where this behavior is being discussed.

@gvanrossum gvanrossum closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in asyncio Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-asyncio
Projects
Status: Done
Development

No branches or pull requests

3 participants