Skip to content

Add TaskGroup.start() and TaskStatus for task readiness signaling#145353

Closed
kovan wants to merge 3 commits intopython:mainfrom
kovan:asyncio-move-on-start
Closed

Add TaskGroup.start() and TaskStatus for task readiness signaling#145353
kovan wants to merge 3 commits intopython:mainfrom
kovan:asyncio-move-on-start

Conversation

@kovan
Copy link

@kovan kovan commented Feb 28, 2026

Summary

  • Add TaskStatus class with started(value=None) method for task readiness signaling
  • Add TaskGroup.start(coro_fn, *args) async method that creates a task and waits until it calls task_status.started(value), returning that value to the caller
  • The task continues running in the group after signaling readiness

This is part of the proposal to adopt proven anyio/Trio patterns natively into asyncio, building on the CancelScope added in #145178. Tracking issue: #145370.

API

async with asyncio.TaskGroup() as tg:
    # start() waits for the task to signal readiness
    address = await tg.start(serve, host, port)
    print(f"Server listening on {address}")
    # task keeps running in the group after started()

The started task receives a TaskStatus object via the task_status keyword argument:

async def serve(host, port, *, task_status):
    server = await setup_server(host, port)
    task_status.started(server.address)  # signal ready, return value to start()
    await server.serve_forever()          # keep running

Test plan

  • test_start_basic — returns value passed to started()
  • test_start_none_valuestarted() with no arg returns None
  • test_start_task_continues — task keeps running after started()
  • test_start_error_before_started — exception before started() propagates
  • test_start_cancelled_before_started — cancellation propagates
  • test_start_already_started_error — calling started() twice raises RuntimeError
  • test_start_multiple_tasks — multiple start() calls in same group
  • test_start_with_namename= forwarded to create_task
  • All existing test_asyncio.test_taskgroups and test_asyncio.test_tasks tests pass

kovan and others added 3 commits February 28, 2026 09:44
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>
@ZeroIntensity
Copy link
Member

See my comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants