Skip to content

Commit a2ceffd

Browse files
[3.13] gh-104091: Suggest TaskGroup for running background tasks in asyncio.create_task() docs (GH-155133) (#155149)
gh-104091: Suggest TaskGroup for running background tasks in asyncio.create_task() docs (GH-155133) (cherry picked from commit 36250a9) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
1 parent 42b23da commit a2ceffd

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

Doc/library/asyncio-task.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ Creating tasks
282282
# completion:
283283
task.add_done_callback(background_tasks.discard)
284284

285+
Note that this approach never awaits the tasks, so if a task
286+
fails, its exception is never retrieved and asyncio logs a
287+
"Task exception was never retrieved" message when the task is
288+
garbage collected. To avoid this, use :class:`asyncio.TaskGroup`
289+
which keeps a strong reference to each task, awaits them and
290+
propagates their exceptions::
291+
292+
async with asyncio.TaskGroup() as tg:
293+
for i in range(10):
294+
tg.create_task(some_coro(param=i))
295+
285296
.. versionadded:: 3.7
286297

287298
.. versionchanged:: 3.8

0 commit comments

Comments
 (0)