File tree 2 files changed +15
-1
lines changed
2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -813,6 +813,7 @@ def _done_callback(fut):
813
813
children = []
814
814
nfuts = 0
815
815
nfinished = 0
816
+ done_futs = []
816
817
loop = None
817
818
outer = None # bpo-46672
818
819
for arg in coros_or_futures :
@@ -829,7 +830,10 @@ def _done_callback(fut):
829
830
830
831
nfuts += 1
831
832
arg_to_fut [arg ] = fut
832
- fut .add_done_callback (_done_callback )
833
+ if fut .done ():
834
+ done_futs .append (fut )
835
+ else :
836
+ fut .add_done_callback (_done_callback )
833
837
834
838
else :
835
839
# There's a duplicate Future object in coros_or_futures.
@@ -838,6 +842,13 @@ def _done_callback(fut):
838
842
children .append (fut )
839
843
840
844
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
+ for fut in done_futs :
851
+ _done_callback (fut )
841
852
return outer
842
853
843
854
Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments