-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-104144: Optimize gather to finish eagerly when all futures complete eagerly #104138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kumaraditya303
merged 8 commits into
python:main
from
itamaro:asyncio-skip-gathering-future
May 6, 2023
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b3e479a
gh-NNNN: Skip creating GatheringFuture if all futures finished eagerly
itamaro 8eeebaf
Add NEWS entry
itamaro 9289c9e
Apply review suggestion to NEWS entry
itamaro edbb04c
Merge branch 'main' into asyncio-skip-gathering-future
itamaro 9be6e51
Modify implementation to eagerly call done callbacks at the end of th…
itamaro 250b73c
update inline comment and NEWS entry
itamaro 7df90eb
Merge branch 'main' into asyncio-skip-gathering-future
itamaro ce52714
Fix NEWS typo
gvanrossum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -815,6 +815,7 @@ def _done_callback(fut): | |||||||
nfinished = 0 | ||||||||
loop = None | ||||||||
outer = None # bpo-46672 | ||||||||
all_finished = True | ||||||||
for arg in coros_or_futures: | ||||||||
if arg not in arg_to_fut: | ||||||||
fut = ensure_future(arg, loop=loop) | ||||||||
|
@@ -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 | ||||||||
itamaro marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
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: | ||||||||
itamaro marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
# optimization: skip creating GatheringFuture if all children completed | ||||||||
# (e.g. when all coros are able to complete eagerly) | ||||||||
outer = futures.Future(loop=loop) | ||||||||
itamaro marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
outer.set_result([c.result for c in children]) | ||||||||
itamaro marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
else: | ||||||||
outer = _GatheringFuture(children, loop=loop) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return outer | ||||||||
|
||||||||
|
||||||||
|
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2023-05-03-16-50-24.gh-issue-104144.yNkjL8.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Optimize asyncio.gather when using eager tasks factory Skip creating | ||
gathering future and scheduling done callbacks when all futures finish | ||
without blocking - for up to 3x speedup | ||
itamaro marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.