GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-104144: Skip scheduling a done callback if a TaskGroup task completes eagerly by itamaro · Pull Request #104140 · python/cpython

Conversation

@itamaro

@itamaro itamaro commented May 3, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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 creating tasks as part of a TaskGroup, e.g.:

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

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

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 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 TaskGroup() was awaited, preventing the task from completing eagerly even if otherwise it could.

applications that use multiple levels of nested TaskGroups can benefit significantly from eagerly completing multiple levels without blocking, as implemented in this PR by skipping scheduling the done callback if the future is done.

Benchmarks

this makes the async pyperformance benchmarks up to 4x faster (!!), using a patch to pyperformance that adds "eager" flavors and uses TaskGroups instead of gather

3.12-base.20230503.async.3.json
===============================

Performance version: 1.0.7
Python version: 3.12.0a7+ (64-bit) revision da1980afcb
Report on Linux-5.15.0-1033-aws-x86_64-with-glibc2.31
Number of logical CPUs: 72
Start date: 2023-05-03 22:18:14.628900
End date: 2023-05-03 22:36:35.397630

3.12-tgcb.20230503.async.3.json
===============================

Performance version: 1.0.7
Python version: 3.12.0a7+ (64-bit) revision 5397cd9f62
Report on Linux-5.15.0-1033-aws-x86_64-with-glibc2.31
Number of logical CPUs: 72
Start date: 2023-05-03 22:38:45.960219
End date: 2023-05-03 22:54:49.008951

+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| Benchmark                     | 3.12-base.20230503.async.3.json | 3.12-tgcb.20230503.async.3.json | Change       | Significance           |
+===============================+=================================+=================================+==============+========================+
| async_tree_cpu_io_mixed       | 843 ms                          | 827 ms                          | 1.02x faster | Not significant        |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_eager              | 383 ms                          | 92.9 ms                         | 4.12x faster | Significant (t=189.26) |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_eager_cpu_io_mixed | 708 ms                          | 412 ms                          | 1.72x faster | Significant (t=180.43) |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_eager_io           | 1.40 sec                        | 1.39 sec                        | 1.01x faster | Not significant        |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_eager_memoization  | 515 ms                          | 232 ms                          | 2.22x faster | Significant (t=183.24) |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_io                 | 1.37 sec                        | 1.35 sec                        | 1.01x faster | Not significant        |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_memoization        | 644 ms                          | 634 ms                          | 1.01x faster | Not significant        |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_none               | 517 ms                          | 508 ms                          | 1.02x faster | Not significant        |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
  • Issue: Leverage eager tasks to optimize asyncio gather & TaskGroups further #104144

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@itamaro
gh-NNNN: Skip scheduling a done callback if a TaskGroup task complete…
5397cd9
…s eagerly
@itamaro itamaro changed the title gh-NNNN: Skip scheduling a done callback if a TaskGroup task completes eagerly gh-104144: Skip scheduling a done callback if a TaskGroup task completes eagerly May 3, 2023
@bedevere-bot bedevere-bot mentioned this pull request May 3, 2023
Leverage eager tasks to optimize asyncio gather & TaskGroups further #104144
Closed
@itamaro
Add NEWS entry
8d6a77e
@itamaro
itamaro marked this pull request as ready for review May 3, 2023 23:52
@itamaro
itamaro requested review from 1st1, asvetlov, gvanrossum, kumaraditya303 and willingc as code owners May 3, 2023 23:52
carljm
carljm reviewed May 4, 2023
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2023-05-03-16-51-53.gh-issue-104144.653Q0P.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@itamaro @carljm
Apply review feedback to NEWS entry
a125c1f
Co-authored-by: Carl Meyer <[email protected]>
@AlexWaygood AlexWaygood added the topic-asyncio label May 4, 2023
@itamaro
Merge branch 'main' into taskgroups-skip-done-callback
dae3e06
gvanrossum
gvanrossum approved these changes May 5, 2023
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

How sure are you that skipping the set-name call if name is None is always correct? (It seems fine to me, but in theory some 3rd party task implementation could do other stuff there.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

itamaro reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-bot bedevere-bot added the awaiting merge label May 5, 2023
@itamaro

itamaro commented May 5, 2023

Copy link
Copy Markdown
Contributor Author

How sure are you that skipping the set-name call if name is None is always correct? (It seems fine to me, but in theory some 3rd party task implementation could do other stuff there.)

I don't know about all possible 3p task implementations. At least looking at uvloop, it seems it uses asyncio.Task directly.
the asyncio Task.set_name returns immediately if name is NULL, so just saving the function call is a pretty minor optimization that we can probably revert with no noticeable regression (I can measure it). If this will make interop with 3p Task implementations better I think it's worth it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented May 5, 2023

Copy link
Copy Markdown
Member

I don't think it will be measurable -- it looks to be a very minor part of creating a task (even more so with gh-103767). It just is one more thing we'd have to put in what's new (and it's not covered by the PR title :-).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@itamaro

itamaro commented May 5, 2023

Copy link
Copy Markdown
Contributor Author

agreed, let me revert that part!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

itamaro added 2 commits May 5, 2023 14:06
@itamaro
revert skipping _set_task_name if name is None; fix typo in comment
161d0f8
@itamaro
Merge branch 'main' into taskgroups-skip-done-callback
2b98bd9
gvanrossum
gvanrossum approved these changes May 5, 2023
View reviewed changes
@gvanrossum
gvanrossum merged commit 52d8f36 into python:main May 5, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label May 5, 2023
kumaraditya303
kumaraditya303 reviewed May 6, 2023
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2023-05-03-16-51-53.gh-issue-104144.653Q0P.rst
@@ -0,0 +1 @@
Optimize :class:`asyncio.TaskGroup` when using :func:`asyncio.eager_task_factory`. Skip scheduling done callbacks when all tasks finish without blocking.

@kumaraditya303 kumaraditya303 May 6, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm actually the scheduling is skipped on a per task basis rather than when all tasks finish without blocking.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@itamaro itamaro May 7, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a fix in itamaro@ac1ee82 (PR depends on gh-104251 first)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@itamaro
itamaro deleted the taskgroups-skip-done-callback branch May 7, 2023 22:16
jbower-fb pushed a commit to jbower-fb/cpython that referenced this pull request May 8, 2023
@itamaro @carljm @jbower-fb
pythongh-104144: Skip scheduling a done callback if a TaskGroup task …
0b9d0b2
…completes eagerly (python#104140)

Co-authored-by: Carl Meyer <[email protected]>
@itamaro itamaro mentioned this pull request Feb 2, 2025
gh-128588: gh-128550: remove eager tasks optimization that missed and introduced incorrect cancellations #129063
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@carljm carljm carljm left review comments
@kumaraditya303 kumaraditya303 kumaraditya303 left review comments
@gvanrossum gvanrossum gvanrossum approved these changes
@1st1 1st1 Awaiting requested review from 1st1 1st1 is a code owner
@asvetlov asvetlov Awaiting requested review from asvetlov asvetlov is a code owner
@willingc willingc Awaiting requested review from willingc willingc is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-asyncio

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@itamaro @gvanrossum @carljm @kumaraditya303 @bedevere-bot @AlexWaygood
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.