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
Changes from1 commit
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
PrevPrevious commit
Reword the message, remove spurious lambda
  • Loading branch information
@ambv
ambv authoredJun 7, 2022
commit98fd62f05caa7ddac90763e7ff403ed3cc8830f4
17 changes: 8 additions & 9 deletionsDoc/library/asyncio-task.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -227,24 +227,23 @@ Creating Tasks

Save a reference to the result of this function, to avoid
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::
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::

# 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.
# Add task totheset. This creates a strong reference.
background_tasks.add(task)

# To preventaccumulation ofreferences toalreadyfinished
#tasks,make each task remove its own reference from set after
# To preventkeepingreferences to finished tasks forever,
# make each task remove its own reference from the set after
# completion:
task.add_done_callback(lambda t:background_tasks.discard(t))
task.add_done_callback(background_tasks.discard)

.. versionadded:: 3.7

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp