Skip to content

gh-136157: Optimize asyncio.to_thread to avoid contextvars.copy_context() overhead for empty contexts #136159

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

heliang666s
Copy link
Contributor

@heliang666s heliang666s commented Jul 1, 2025

@bedevere-app
Copy link

bedevere-app bot commented Jul 1, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@heliang666s heliang666s changed the title gh136157:Optimize asyncio.to_thread to avoid contextvars.copy_context() overhead for empty contexts gh-136157:Optimize asyncio.to_thread to avoid contextvars.copy_context() overhead for empty contexts Jul 1, 2025
@bedevere-app
Copy link

bedevere-app bot commented Jul 1, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz picnixz changed the title gh-136157:Optimize asyncio.to_thread to avoid contextvars.copy_context() overhead for empty contexts gh-136157: Optimize asyncio.to_thread to avoid contextvars.copy_context() overhead for empty contexts Jul 1, 2025
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Please provide some benchmarks.

Comment on lines +24 to +27
if not ctx:
callback = functools.partial(func, *args, **kwargs)
else:
callback = functools.partial(ctx.run, func, *args, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

Hm, couldn't we alternatively add a fast path to Context.run?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. Please see the issue for details.

@ZeroIntensity
Copy link
Member

Please provide some benchmarks.

Ah, oops, didn't look at the issue. It would be helpful to put them in the PR description as well.

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