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

Skip to content
Merged
Next Next commit
gh-NNNN: Skip creating GatheringFuture if all futures finished eagerly
  • Loading branch information
itamaro committed May 3, 2023
commit b3e479ac2830642ba6d4dcf22259e22a8145a2d9
16 changes: 14 additions & 2 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ def _done_callback(fut):
nfinished = 0
loop = None
outer = None # bpo-46672
all_finished = True
Comment thread
itamaro marked this conversation as resolved.
Outdated
for arg in coros_or_futures:
if arg not in arg_to_fut:
fut = ensure_future(arg, loop=loop)
Expand All @@ -829,15 +830,26 @@ def _done_callback(fut):

nfuts += 1
arg_to_fut[arg] = fut
fut.add_done_callback(_done_callback)
if fut.done():
# call the callback immediately instead of scheduling it
_done_callback(fut)
else:
all_finished = False
Comment thread
itamaro marked this conversation as resolved.
Outdated
fut.add_done_callback(_done_callback)

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

children.append(fut)

outer = _GatheringFuture(children, loop=loop)
if all_finished:
Comment thread
itamaro marked this conversation as resolved.
Outdated
# optimization: skip creating GatheringFuture if all children completed
# (e.g. when all coros are able to complete eagerly)
outer = futures.Future(loop=loop)
Comment thread
itamaro marked this conversation as resolved.
Outdated
outer.set_result([c.result for c in children])
Comment thread
itamaro marked this conversation as resolved.
Outdated
else:
outer = _GatheringFuture(children, loop=loop)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
outer = _GatheringFuture(children, loop=loop)
outer.__self_log_traceback = False
outer = _GatheringFuture(children, loop=loop)

return outer


Expand Down