mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
gh-104144: Skip scheduling a done callback if a TaskGroup task completes eagerly (#104140)
Co-authored-by: Carl Meyer <carl@oddbird.net>
This commit is contained in:
parent
f3e7eb48f8
commit
52d8f36e8c
2 changed files with 9 additions and 2 deletions
|
@ -164,8 +164,14 @@ class TaskGroup:
|
||||||
else:
|
else:
|
||||||
task = self._loop.create_task(coro, context=context)
|
task = self._loop.create_task(coro, context=context)
|
||||||
tasks._set_task_name(task, name)
|
tasks._set_task_name(task, name)
|
||||||
task.add_done_callback(self._on_task_done)
|
# optimization: Immediately call the done callback if the task is
|
||||||
self._tasks.add(task)
|
# already done (e.g. if the coro was able to complete eagerly),
|
||||||
|
# and skip scheduling a done callback
|
||||||
|
if task.done():
|
||||||
|
self._on_task_done(task)
|
||||||
|
else:
|
||||||
|
self._tasks.add(task)
|
||||||
|
task.add_done_callback(self._on_task_done)
|
||||||
return task
|
return task
|
||||||
|
|
||||||
# Since Python 3.8 Tasks propagate all exceptions correctly,
|
# Since Python 3.8 Tasks propagate all exceptions correctly,
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Optimize :class:`asyncio.TaskGroup` when using :func:`asyncio.eager_task_factory`. Skip scheduling done callbacks when all tasks finish without blocking.
|
Loading…
Add table
Add a link
Reference in a new issue