Add TaskGroup.start() and TaskStatus for task readiness signaling#145353
Closed
kovan wants to merge 3 commits intopython:mainfrom
Closed
Add TaskGroup.start() and TaskStatus for task readiness signaling#145353kovan wants to merge 3 commits intopython:mainfrom
kovan wants to merge 3 commits intopython:mainfrom
Conversation
Introduce CancelScope, an async context manager that provides level-triggered cancellation semantics for asyncio. Once cancelled (via cancel() or deadline expiry), every subsequent await inside the scope raises CancelledError until the scope exits — the coroutine cannot simply catch-and-ignore. This integrates with Task.__step by checking the current scope's state after the existing edge-triggered _must_cancel check. The scope pushes/pops itself on a per-task _current_cancel_scope linked-list stack. Existing edge-triggered mechanisms (cancel(), uncancel(), Timeout, TaskGroup) remain completely unchanged. New public API: - asyncio.CancelScope(*, deadline=None, shield=False) - asyncio.cancel_scope(delay, *, shield=False) - asyncio.cancel_scope_at(when, *, shield=False) Both the Python (_PyTask) and C (_CTask) Task implementations are updated. Code not using CancelScope has zero overhead (_current_cancel_scope is None → single pointer check). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add TaskStatus class with started() method and TaskGroup.start() async method. The task calls task_status.started(value) to signal it is ready, and start() returns that value to the caller. The task continues running in the group after signaling readiness. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
|
See my comment. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
TaskStatusclass withstarted(value=None)method for task readiness signalingTaskGroup.start(coro_fn, *args)async method that creates a task and waits until it callstask_status.started(value), returning that value to the callerThis is part of the proposal to adopt proven anyio/Trio patterns natively into asyncio, building on the
CancelScopeadded in #145178. Tracking issue: #145370.API
The started task receives a
TaskStatusobject via thetask_statuskeyword argument:Test plan
test_start_basic— returns value passed tostarted()test_start_none_value—started()with no arg returnsNonetest_start_task_continues— task keeps running afterstarted()test_start_error_before_started— exception beforestarted()propagatestest_start_cancelled_before_started— cancellation propagatestest_start_already_started_error— callingstarted()twice raisesRuntimeErrortest_start_multiple_tasks— multiplestart()calls in same grouptest_start_with_name—name=forwarded tocreate_tasktest_asyncio.test_taskgroupsandtest_asyncio.test_taskstests pass