Skip to content

Commit

Permalink
gh-104144: Optimize gather to finish eagerly when all futures complet…
Browse files Browse the repository at this point in the history
…e eagerly (#104138)
  • Loading branch information
itamaro authored May 6, 2023
1 parent 96f95df commit 263abd3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ def _done_callback(fut):
children = []
nfuts = 0
nfinished = 0
done_futs = []
loop = None
outer = None # bpo-46672
for arg in coros_or_futures:
Expand All @@ -829,7 +830,10 @@ def _done_callback(fut):

nfuts += 1
arg_to_fut[arg] = fut
fut.add_done_callback(_done_callback)
if fut.done():
done_futs.append(fut)
else:
fut.add_done_callback(_done_callback)

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

outer = _GatheringFuture(children, loop=loop)
# Run done callbacks after GatheringFuture created so any post-processing
# can be performed at this point
# optimization: in the special case that *all* futures finished eagerly,
# this will effectively complete the gather eagerly, with the last
# callback setting the result (or exception) on outer before returning it
for fut in done_futs:
_done_callback(fut)
return outer


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Optimize :func:`asyncio.gather` when using :func:`asyncio.eager_task_factory`
to complete eagerly if all fututres completed eagerly.
Avoid scheduling done callbacks for futures that complete eagerly.

0 comments on commit 263abd3

Please sign in to comment.