Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
4a5d42c tob3e479aCompare
carljm left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This looks pretty straightforward and reasonable to me, but would prefer for asyncio experts to take a look.
Misc/NEWS.d/next/Library/2023-05-03-16-50-24.gh-issue-104144.yNkjL8.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Carl Meyer <carl@oddbird.net>
jbower-fb left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
If you look atgather._done_callback() you'll see it has a bunch of logic which gets executed at the moment all the futures have finished (starting fromif nfinished == nfuts: on line 781). If all args can complete eagerly then this will be executed eagerly too.
This might have a few issues:
- At this point
outerwill beNoneand this will cause trouble on line 803. (Maybe I missed something because I'm surprised this hasn't come up yet). - Handling for futures that were cancelled during eager execution is processed but the results discarded.
- We inefficiently create a result list which we discard and then repeat when creating the eagerly completed future result.
Fortunately, Ithink an easy fix is to move creation of the eager result future to before the argument processing loop. See my in-line comments for specifics.
I'm not 100% sure how this will affect the issue described inbpo-46672, but it has a test so we'll see.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Lib/asyncio/tasks.py Outdated
| outer=futures.Future(loop=loop) | ||
| outer.set_result([c.resultforcinchildren]) | ||
| else: | ||
| outer=_GatheringFuture(children,loop=loop) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
| outer=_GatheringFuture(children,loop=loop) | |
| outer.__self_log_traceback=False | |
| outer=_GatheringFuture(children,loop=loop) |
…e loop, after the GatheringFuture (outer) is created
carljm commentedMay 5, 2023
The title no longer accurately describes the updated PR, since the |
itamaro commentedMay 5, 2023
thanks for the review@jbower-fb ! I pushed a new version of the PR based on your suggestions, but not identical.
|
itamaro commentedMay 5, 2023
thanks, I updated the title! |
gvanrossum left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM, will fix the typo. Maybe@carljm can merge when you all are agreed on this. Nice fix!
Misc/NEWS.d/next/Library/2023-05-03-16-50-24.gh-issue-104144.yNkjL8.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
gvanrossum commentedMay 6, 2023
PS. Ingeneral there's no need to click the "Update branch" button (or otherwise merge main back into the PR)unless there are fixes/changes that might affect the PR (e.g. if touching the same file). |
Uh oh!
There was an error while loading.Please reload this page.
gh-97696 introduced eager tasks factory, which speeds up some async-heavy workloads by up to 50% when opted in.
installing the eager tasks factory applies out-of-the-box when gathering futures (
asyncio.gather(...)), e.g.:coro{1,2,3}will eagerly execute the first step, and potentially complete without scheduling to the event loop if the coros don't block.the implementation of
eageruses callbacks internally that end up getting scheduled to the event loop even if all the futures were able to finish synchronously, and blocking the coroutine in whichgather()was awaited, preventing the task from completing eagerly even if otherwise it could.applications that use multiple levels of nested gathers can benefit significantly from eagerly completing multiple levels without blocking, as implemented in this PR by skipping scheduling done callbacks for futures that are already done (e.g. finished eagerly).
Benchmarks
this makes the async pyperformance benchmarks up to 3x faster (!!), using apatch to pyperformance that adds "eager" flavors