summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorItamar Ostricher <itamarost@gmail.com>2023-05-05 23:44:03 (GMT)
committerGitHub <noreply@github.com>2023-05-05 23:44:03 (GMT)
commit52d8f36e8c9f6048367d7bdfede3698e3f5f70d0 (patch)
tree69e07fa1186a2886ba38e9c75e765511490386e5 /Lib/asyncio
parentf3e7eb48f86057919c347f56dabf417acfd55845 (diff)
downloadcpython-52d8f36e8c9f6048367d7bdfede3698e3f5f70d0.zip
cpython-52d8f36e8c9f6048367d7bdfede3698e3f5f70d0.tar.gz
cpython-52d8f36e8c9f6048367d7bdfede3698e3f5f70d0.tar.bz2
gh-104144: Skip scheduling a done callback if a TaskGroup task completes eagerly (#104140)
Co-authored-by: Carl Meyer <carl@oddbird.net>
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/taskgroups.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py
index 0fdea36..06b2e0d 100644
--- a/Lib/asyncio/taskgroups.py
+++ b/Lib/asyncio/taskgroups.py
@@ -164,8 +164,14 @@ class TaskGroup:
else:
task = self._loop.create_task(coro, context=context)
tasks._set_task_name(task, name)
- task.add_done_callback(self._on_task_done)
- self._tasks.add(task)
+ # optimization: Immediately call the done callback if the task is
+ # 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
# Since Python 3.8 Tasks propagate all exceptions correctly,