Leverage eager tasks to optimize asyncio gather & TaskGroups further #104144
Labels
3.12
only security fixes
performance
Performance or resource usage
topic-asyncio
type-feature
A feature request or enhancement
Feature or enhancement
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 tasks (
asyncio.gather(...)
), and when creating tasks as part of aTaskGroup
, e.g.:in both examples,
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 both
gather
andTaskGroup
uses callbacks internally that end up getting scheduled to the event loop even if all the tasks were able to finish synchronously, and blocking the coroutine in whichgather
orTaskGroup()
was awaited, preventing from the task to complete eagerly even if otherwise it could.applications that use multiple levels of nested gathers / TaskGroups can benefit significantly from eagerly completing multiple levels without blocking.
this can be achieved by modifying the implementation of
gather
andTaskGroup
to detect when it can complete without blocking, and skip scheduling wakeup & done callbacks.Linked PRs
The text was updated successfully, but these errors were encountered: