Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4a5d42c

Browse files
committed
gh-NNNN: Skip creating GatheringFuture if all futures finished eagerly
1 parent da1980a commit 4a5d42c

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Lib/asyncio/tasks.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ def _done_callback(fut):
815815
nfinished = 0
816816
loop = None
817817
outer = None # bpo-46672
818+
all_finished = True
818819
for arg in coros_or_futures:
819820
if arg not in arg_to_fut:
820821
fut = ensure_future(arg, loop=loop)
@@ -829,15 +830,23 @@ def _done_callback(fut):
829830

830831
nfuts += 1
831832
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)
833836

834837
else:
835838
# There's a duplicate Future object in coros_or_futures.
836839
fut = arg_to_fut[arg]
837840

838841
children.append(fut)
839842

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)
841850
return outer
842851

843852

0 commit comments

Comments
 (0)