File tree 1 file changed +14
-2
lines changed
1 file changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -815,6 +815,7 @@ def _done_callback(fut):
815
815
nfinished = 0
816
816
loop = None
817
817
outer = None # bpo-46672
818
+ all_finished = True
818
819
for arg in coros_or_futures :
819
820
if arg not in arg_to_fut :
820
821
fut = ensure_future (arg , loop = loop )
@@ -829,15 +830,26 @@ 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
+ # call the callback immediately instead of scheduling it
835
+ _done_callback (fut )
836
+ else :
837
+ all_finished = False
838
+ fut .add_done_callback (_done_callback )
833
839
834
840
else :
835
841
# There's a duplicate Future object in coros_or_futures.
836
842
fut = arg_to_fut [arg ]
837
843
838
844
children .append (fut )
839
845
840
- outer = _GatheringFuture (children , loop = loop )
846
+ if all_finished :
847
+ # optimization: skip creating GatheringFuture if all children completed
848
+ # (e.g. when all coros are able to complete eagerly)
849
+ outer = futures .Future (loop = loop )
850
+ outer .set_result ([c .result for c in children ])
851
+ else :
852
+ outer = _GatheringFuture (children , loop = loop )
841
853
return outer
842
854
843
855
You can’t perform that action at this time.
0 commit comments