mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-104144: Optimize gather to finish eagerly when all futures complete eagerly (#104138)
This commit is contained in:
parent
96f95df48e
commit
263abd333d
2 changed files with 15 additions and 1 deletions
|
@ -813,6 +813,7 @@ def gather(*coros_or_futures, return_exceptions=False):
|
||||||
children = []
|
children = []
|
||||||
nfuts = 0
|
nfuts = 0
|
||||||
nfinished = 0
|
nfinished = 0
|
||||||
|
done_futs = []
|
||||||
loop = None
|
loop = None
|
||||||
outer = None # bpo-46672
|
outer = None # bpo-46672
|
||||||
for arg in coros_or_futures:
|
for arg in coros_or_futures:
|
||||||
|
@ -829,7 +830,10 @@ def gather(*coros_or_futures, return_exceptions=False):
|
||||||
|
|
||||||
nfuts += 1
|
nfuts += 1
|
||||||
arg_to_fut[arg] = fut
|
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:
|
else:
|
||||||
# There's a duplicate Future object in coros_or_futures.
|
# There's a duplicate Future object in coros_or_futures.
|
||||||
|
@ -838,6 +842,13 @@ def gather(*coros_or_futures, return_exceptions=False):
|
||||||
children.append(fut)
|
children.append(fut)
|
||||||
|
|
||||||
outer = _GatheringFuture(children, loop=loop)
|
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
|
return outer
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue