8000 gh-94972: document that shield users need to keep a reference to thei… · python/cpython@9b71058 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b71058

Browse files
gh-94972: document that shield users need to keep a reference to their task (GH-96724)
Co-authored-by: Thomas Grainger <tagrain@gmail.com> Co-authored-by: Guido van Rossum <gvanrossum@gmail.com> (cherry picked from commit 6281aff) Co-authored-by: Hendrik Makait <hendrik.makait@gmail.com>
1 parent 5a17200 commit 9b71058

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

Doc/library/asyncio-future.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Future Functions
5555
preferred way for creating new Tasks.
5656

5757
Save a reference to the result of this function, to avoid
58-
a task disappearing mid execution.
58+
a task disappearing mid-execution.
5959

6060
.. versionchanged:: 3.5.1
6161
The function accepts any :term:`awaitable` object.

Doc/library/asyncio-task.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ Creating Tasks
262262
.. important::
263263

264264
Save a reference to the result of this function, to avoid
265-
a task disappearing mid execution. The event loop only keeps
265+
a task disappearing mid-execution. The event loop only keeps
266266
weak references to tasks. A task that isn't referenced elsewhere
267-
may get garbage-collected at any time, even before it's done.
267+
may get garbage collected at any time, even before it's done.
268268
For reliable "fire-and-forget" background tasks, gather them in
269269
a collection::
270270

@@ -441,7 +441,8 @@ Shielding From Cancellation
441441

442442
The statement::
443443

444-
res = await shield(something())
444+
task = asyncio.create_task(something())
445+
res = await shield(task)
445446

446447
is equivalent to::
447448

@@ -460,11 +461,19 @@ Shielding From Cancellation
460461
the ``shield()`` function should be combined with a try/except
461462
clause, as follows::
462463

464+
task = asyncio.create_task(something())
463465
try:
464-
res = await shield(something())
466+
res = await shield(task)
465467
except CancelledError:
466468
res = None
467469

470+
.. important::
471+
472+
Save a reference to tasks passed to this function, to avoid
473+
a task disappearing mid-execution. The event loop only keeps
474+
weak references to tasks. A task that isn't referenced elsewhere
475+
may get garbage collected at any time, even before it's done.
476+
468477
.. versionchanged:: 3.10
469478
Removed the *loop* parameter.
470479

Lib/asyncio/tasks.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,8 @@ def shield(arg):
809809
810810
The statement
811811
812-
res = await shield(something())
812+
task = asyncio.create_task(something())
813+
res = await shield(task)
813814
814815
is exactly equivalent to the statement
815816
@@ -825,10 +826,16 @@ def shield(arg):
825826
If you want to completely ignore cancellation (not recommended)
826827
you can combine shield() with a try/except clause, as follows:
827828
829+
task = asyncio.create_task(something())
828830
try:
829-
res = await shield(something())
831+
res = await shield(task)
830832
except CancelledError:
831833
res = None
834+
835+
Save a reference to tasks passed to this function, to avoid
836+
a task disappearing mid-execution. The event loop only keeps
837+
weak references to tasks. A task that isn't referenced elsewhere
838+
may get garbage collected at any time, even before it's done.
832839
"""
833840
inner = _ensure_future(arg)
834841
if inner.done():

0 commit comments

Comments
 (0)
0