Skip to content

Commit 6281aff

Browse files
hendrikmakaitgraingertgvanrossum
authored
gh-94972: document that shield users need to keep a reference to their task (#96724)
Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: Guido van Rossum <[email protected]>
1 parent 50a70a0 commit 6281aff

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

Doc/library/asyncio-future.rst

+1-1
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

+13-4
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ Creating Tasks
254254
.. important::
255255

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

@@ -520,7 +520,8 @@ Shielding From Cancellation
520520

521521
The statement::
522522

523-
res = await shield(something())
523+
task = asyncio.create_task(something())
524+
res = await shield(task)
524525

525526
is equivalent to::
526527

@@ -539,11 +540,19 @@ Shielding From Cancellation
539540
the ``shield()`` function should be combined with a try/except
540541
clause, as follows::
541542

543+
task = asyncio.create_task(something())
542544
try:
543-
res = await shield(something())
545+
res = await shield(task)
544546
except CancelledError:
545547
res = None
546548

549+
.. important::
550+
551+
Save a reference to tasks passed to this function, to avoid
552+
a task disappearing mid-execution. The event loop only keeps
553+
weak references to tasks. A task that isn't referenced elsewhere
554+
may get garbage collected at any time, even before it's done.
555+
547556
.. versionchanged:: 3.10
548557
Removed the *loop* parameter.
549558

Lib/asyncio/tasks.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,8 @@ def shield(arg):
848848
849849
The statement
850850
851-
res = await shield(something())
851+
task = asyncio.create_task(something())
852+
res = await shield(task)
852853
853854
is exactly equivalent to the statement
854855
@@ -864,10 +865,16 @@ def shield(arg):
864865
If you want to completely ignore cancellation (not recommended)
865866
you can combine shield() with a try/except clause, as follows:
866867
868+
task = asyncio.create_task(something())
867869
try:
868-
res = await shield(something())
870+
res = await shield(task)
869871
except CancelledError:
870872
res = None
873+
874+
Save a reference to tasks passed to this function, to avoid
875+
a task disappearing mid-execution. The event loop only keeps
876+
weak references to tasks. A task that isn't referenced elsewhere
877+
may get garbage collected at any time, even before it's done.
871878
"""
872879
inner = _ensure_future(arg)
873880
if inner.done():

0 commit comments

Comments
 (0)