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

New threads should inherit the contextvars.Context of their creator #128555

Open
nascheme opened this issue Jan 6, 2025 · 2 comments
Open

New threads should inherit the contextvars.Context of their creator #128555

nascheme opened this issue Jan 6, 2025 · 2 comments
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@nascheme
Copy link
Member

nascheme commented Jan 6, 2025

Feature or enhancement

Proposal:

It would be convenient and likely less surprising if threads inherited the context of their creator, rather than starting with an empty context. This would match what asynio.Task does. This has been requested before, as gh-86981.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/thread-safe-warning-filters/

Linked PRs

@nascheme nascheme added the type-feature A feature request or enhancement label Jan 6, 2025
@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) extension-modules C modules in the Modules dir labels Jan 6, 2025
@rhettinger
Copy link
Contributor

It would be convenient and likely less surprising if threads inherited the context of their creator, rather than starting with an empty context.

My mental model for context vars is that they operate similarly to thread.local() and that it would be surprising if they didn't.

@StanSvec
Copy link

StanSvec commented Jan 9, 2025

+1 on this idea. When a parent task needs to split work into concurrent subtasks, having access to the same context just makes sense. I can't see any real downsides to doing this, but I can think of many cases where inherited context would be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants