This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bernat
Recipients bernat, docs@python
Date 2021-07-18.06:01:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1626588086.28.0.802457585502.issue44665@roundup.psfhosted.org>
In-reply-to
Content
asyncio will only keep weak references to alive tasks (in `_all_tasks`). If a user does not keep a reference to a task and the task is not currently executing or sleeping, the user may get "Task was destroyed but it is pending!".

I would suggest adding the following paragraph to `create_task()` documentation:

Python only keeps weak references to the scheduled tasks. To avoid the task being destroyed by the garbage collector while still pending, a reference to it should be kept until the task is done.

And maybe an example in case a user wants something "fire and forget"?

```python
running_tasks = set()
# [...]
task = asyncio.create_task(some_background_function())
running_tasks.add(task)
task.add_done_callback(lambda t: running_tasks.remove(t))
```

The same applies to ensure_future as it now uses create_task, so maybe a "See create_task()".
History
Date User Action Args
2021-07-18 06:01:26bernatsetrecipients: + bernat, docs@python
2021-07-18 06:01:26bernatsetmessageid: <1626588086.28.0.802457585502.issue44665@roundup.psfhosted.org>
2021-07-18 06:01:26bernatlinkissue44665 messages
2021-07-18 06:01:25bernatcreate