Skip to content
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

Leverage eager tasks to optimize asyncio gather & TaskGroups further #104144

Closed
itamaro opened this issue May 3, 2023 · 0 comments
Closed

Leverage eager tasks to optimize asyncio gather & TaskGroups further #104144

itamaro opened this issue May 3, 2023 · 0 comments
Labels
3.12 bugs and security fixes performance Performance or resource usage topic-asyncio type-feature A feature request or enhancement

Comments

@itamaro
Copy link
Contributor

itamaro commented May 3, 2023

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 a TaskGroup, e.g.:

asyncio.get_event_loop().set_task_factory(asyncio.eager_task_factory)

await asyncio.gather(coro1, coro2, coro3)

async with asyncio.TaskGroup() as tg:
    tg.create_task(coro1)
    tg.create_task(coro2)
    tg.create_task(coro3)

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 and TaskGroup 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 which gather or TaskGroup() 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 and TaskGroup to detect when it can complete without blocking, and skip scheduling wakeup & done callbacks.

Linked PRs

@itamaro itamaro added the type-feature A feature request or enhancement label May 3, 2023
gvanrossum pushed a commit that referenced this issue May 5, 2023
…tes eagerly (#104140)

Co-authored-by: Carl Meyer <carl@oddbird.net>
@kumaraditya303 kumaraditya303 added performance Performance or resource usage topic-asyncio 3.12 bugs and security fixes labels May 6, 2023
jbower-fb pushed a commit to jbower-fb/cpython-jbowerfb that referenced this issue May 8, 2023
…completes eagerly (python#104140)

Co-authored-by: Carl Meyer <carl@oddbird.net>
jbower-fb pushed a commit to jbower-fb/cpython-jbowerfb that referenced this issue May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes performance Performance or resource usage topic-asyncio type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants