File tree 1 file changed +11
-2
lines changed
1 file changed +11
-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,23 @@ 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 not fut .done ():
834
+ all_finished = False
835
+ fut .add_done_callback (_done_callback )
833
836
834
837
else :
835
838
# There's a duplicate Future object in coros_or_futures.
836
839
fut = arg_to_fut [arg ]
837
840
838
841
children .append (fut )
839
842
840
- outer = _GatheringFuture (children , loop = loop )
843
+ if all_finished :
844
+ # optimization: skip creating GatheringFuture if all children are done
845
+ # (e.g. when all coros are able to complete eagerly)
846
+ outer = futures .Future (loop = loop )
847
+ outer .set_result (c .result for c in children )
848
+ else :
849
+ outer = _GatheringFuture (children , loop = loop )
841
850
return outer
842
851
843
852
You can’t perform that action at this time.
0 commit comments