-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Introduce task groups to asyncio and change task cancellation semantics #90908
Comments
After some conversations with Yury, and encouraged by the SC's approval of PEP-654, I am proposing to add a new class, asyncio.TaskGroup, which introduces structured concurrency similar to nurseries in Trio. I started with EdgeDb's TaskGroup implementation (https://github.com/edgedb/edgedb/blob/master/edb/common/taskgroup.py) and tweaked it only slightly. I also changed a few things in asyncio.Task (see below). The key change I made to EdgeDb's TaskGroup is that subtasks can keep spawning more subtasks while __aexit__ is running; __aexit__ exits once the last subtask is done. I made this change after consulting some Trio folks, who knew of real-world use cases for this behavior, and did not know of real-world code in need of prohibiting task creation as soon as __aexit__ starts running. I added some tests for the new behavior; none of the existing tests needed to be adjusted to accommodate this change. (For other changes relative to the EdgeDb's TaskGroup, see #75453.) In order to avoid the need to monkey-patch the parent task, I added two new methods to asyncio.Task, .cancelled() and .uncancel(), that manage a flag corresponding to __cancel_requested__ in EdgeDb's TaskGroup. **This introduces a change in behavior around task cancellation:**
This change in semantics did not cause any asyncio unittests to fail. However, it may be surprising (especially to Trio folks, where the semantics are pretty much the opposite, once a Trio task is cancelled all further await calls in that task fail unless explicitly shielded). For the TaskGroup tests to pass, we require a flag that is not cleared. However, it is probably not really required to ignore subsequent .cancel() calls until .uncancel() is called. This just seemed more consistent, and it is what @asvetlov proposed above and implemented in #75494 (using a property .__cancel_requested__ as the API). |
Remaining TODO list:
(We could also add something like Trio's cancel scopes, e.g. based on |
For your TODO list (not sure how else to communicate this): I agree with the de-emphasis of gather(). I think adding another version of gather() that cancels all the remaining tasks if one fails would also be good, unless you think it is completely redundant due to TaskGroups. This idea was originally mentioned in https://bugs.python.org/issue31452 as a bug, and determined to be "works as designed". So now making an all-cancel() version of gather() is an idiom that people keep recoding, e.g. https://stackoverflow.com/questions/59073556/how-to-cancel-all-remaining-tasks-in-gather-if-one-fails. |
@dhalbert, it's probably better to file a new issue if you want changes to gather(). Although I suppose that if we want to deemphasize it, we shouldn't be adding new features to it. My own new feature idea would be to have it wait for all tasks and then if there are any exceptions, raise an ExceptionGroup. That (like any new gather() behaviors) would require a new keyword-only flag to gather(). If we're going to deemphasize it I might not bother though. There's one thing that gather() does that TaskGroup doesn't: it gives us the return values from the tasks. The question is whether that's useful. If it is maybe we should *not* deepmhasize gather() quite as much and then adding new features would be okay. |
I've created a separate issue for cancel scopes: bpo-46771. |
That's easy to do with task groups too: async with TaskGroup() as g:
r1 = g.create_task(coro1())
r2 = g.create_task(coro2())
print(r1.result())
# or
print(await r2) # I *think* this should work |
I have a PR up to typeshed to add the new Task methods and a new stub file taskgroups.pyi: python/typeshed#7240 |
Is there a plan or PR yet for documenting |
No, that’s why this issue is still open. |
Adding deferred blocker as it would be nice to have docs before the rc. |
I'll try to write docs for taskgroups. |
We should also document the new Task.uncancel() method, and the subtly new cancellation semantics in general. I'd like to do that in a separate PR: unlike |
Co-authored-by: CAM Gerlach <[email protected]>
Co-authored-by: CAM Gerlach <[email protected]> (cherry picked from commit b6ec6d4) Co-authored-by: Guido van Rossum <[email protected]>
Co-authored-by: CAM Gerlach <[email protected]> (cherry picked from commit b6ec6d4) Co-authored-by: Guido van Rossum <[email protected]>
Now the docs are up we can close this. |
Co-authored-by: CAM Gerlach <[email protected]>
…l() (#95253) Co-authored-by: Thomas Grainger <[email protected]>
…ncancel() (pythonGH-95253) Co-authored-by: Thomas Grainger <[email protected]> (cherry picked from commit f00645d) Co-authored-by: Łukasz Langa <[email protected]>
…l() (GH-95253) Co-authored-by: Thomas Grainger <[email protected]> (cherry picked from commit f00645d) Co-authored-by: Łukasz Langa <[email protected]>
…ncancel() (python#95253) Co-authored-by: Thomas Grainger <[email protected]>
…l() (GH-95253) Co-authored-by: Thomas Grainger <[email protected]> (cherry picked from commit f00645d) Co-authored-by: Łukasz Langa <[email protected]>
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: