Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-88831: In docs for asyncio.create_task, explain why strong references to tasks are needed#93258

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ambv merged 4 commits intopython:mainfromagrommek:augment_asyncio_create_task_docs
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletionDoc/library/asyncio-task.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,7 +226,25 @@ Creating Tasks
.. important::

Save a reference to the result of this function, to avoid
a task disappearing mid execution.
a task disappearing mid execution. The event loop only keeps
weak references to all task. Without a strong reference, the
task may get garbage-collected at any time. If you want to have
"fire-and-forget" background tasks you can do something like this::

# create an empty set to store references to background tasks
background_tasks = set()

# start 10 background tasks
for i in range(10):
task = asyncio.create_task(some_coro(param=i))

# Add task to set. This creates a strong reference.
background_tasks.add(task)

# To prevent accumulation of references to already finished
# tasks, make each task remove its own reference from set after
# completion:
task.add_done_callback(lambda t: background_tasks.discard(t))

.. versionadded:: 3.7

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Augmented documentation of asyncio.create_task(). Clarified the need to keep strong references to tasks and added a code snippet detailing how to to this.

[8]ページ先頭

©2009-2025 Movatter.jp