Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit263abd3

Browse files
authored
gh-104144: Optimize gather to finish eagerly when all futures complete eagerly (#104138)
1 parent96f95df commit263abd3

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

‎Lib/asyncio/tasks.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ def _done_callback(fut):
813813
children= []
814814
nfuts=0
815815
nfinished=0
816+
done_futs= []
816817
loop=None
817818
outer=None# bpo-46672
818819
forargincoros_or_futures:
@@ -829,7 +830,10 @@ def _done_callback(fut):
829830

830831
nfuts+=1
831832
arg_to_fut[arg]=fut
832-
fut.add_done_callback(_done_callback)
833+
iffut.done():
834+
done_futs.append(fut)
835+
else:
836+
fut.add_done_callback(_done_callback)
833837

834838
else:
835839
# There's a duplicate Future object in coros_or_futures.
@@ -838,6 +842,13 @@ def _done_callback(fut):
838842
children.append(fut)
839843

840844
outer=_GatheringFuture(children,loop=loop)
845+
# Run done callbacks after GatheringFuture created so any post-processing
846+
# can be performed at this point
847+
# optimization: in the special case that *all* futures finished eagerly,
848+
# this will effectively complete the gather eagerly, with the last
849+
# callback setting the result (or exception) on outer before returning it
850+
forfutindone_futs:
851+
_done_callback(fut)
841852
returnouter
842853

843854

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Optimize:func:`asyncio.gather` when using:func:`asyncio.eager_task_factory`
2+
to complete eagerly if all fututres completed eagerly.
3+
Avoid scheduling done callbacks for futures that complete eagerly.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp