Skip to content

Commit 55e9f46

Browse files
kumaraditya303miss-islington
authored andcommitted
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 d768d94 commit 55e9f46

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
@@ -288,6 +288,17 @@ Creating tasks
288288
# completion:
289289
task.add_done_callback(background_tasks.discard)
290290

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

293304
.. versionchanged:: 3.8

0 commit comments

Comments
 (0)