-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-128555: Add 'context' keyword parameter to Thread. #128209
base: main
Are you sure you want to change the base?
Conversation
By default, inherit the context from the thread calling `Thread.start()`.
708127d
to
808fa72
Compare
This matches what asyncio-task does.
I wonder if |
Doc/library/threading.rst
Outdated
@@ -369,6 +374,10 @@ since it is impossible to detect the termination of alien threads. | |||
.. versionchanged:: 3.10 | |||
Use the *target* name if *name* argument is omitted. | |||
|
|||
.. versionchanged:: 3.14 | |||
Added the *context* parameter. Previously threads always ran with an empty |
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 think I'd make this more direct about the behavior change, perhaps something like:
"Threads now inherit the context of the caller of Thread.start instead of starting with an empty context. The context parameter was added. Pass a new contextvars.Context() if your thread requires an empty context."
This default change is a behavior change. Expect that to trip someones existing code up. I don't have the context (pun intended) as to how disruptive that could be. Per the PEP-387 breaking change policy we'd want to wait at least two releases. Which isn't so satisfying given the reasons you want this.
But a compromise could be considered (unsure if this is really wise) if needed: Change the default sooner than the deprecation when running in an experimental free-threading cpython build?
Co-authored-by: Kumar Aditya <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
By default, inherit the context from the thread calling
Thread.start()
.This was motivated by making the
warnings
module work more reliably when threads and asyncio are used in combination withcatch_warnings
. I'm working on a PR that will makewarnings
use a contextvar for the filtering state.This is not a new idea, it was suggested around the 3.10 release timeframe, gh-86981.
📚 Documentation preview 📚: https://cpython-previews--128209.org.readthedocs.build/