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

gh-128555: Add 'context' keyword parameter to Thread. #128209

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

nascheme
Copy link
Member

@nascheme nascheme commented Dec 24, 2024

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 with catch_warnings. I'm working on a PR that will make warnings 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/

By default, inherit the context from the thread calling
`Thread.start()`.
@nascheme nascheme force-pushed the thread_inherit_context branch from 708127d to 808fa72 Compare December 28, 2024 21:05
This matches what asyncio-task does.
@nascheme
Copy link
Member Author

nascheme commented Jan 3, 2025

I wonder if threading.Thread is the correct layer to do this at. Perhaps it should be done at a lower level, e.g. inside Modules/_threadmodule.c.

@@ -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
Copy link
Member

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?

Doc/library/threading.rst Outdated Show resolved Hide resolved
Lib/threading.py Outdated Show resolved Hide resolved
@nascheme nascheme changed the title Add 'context' keyword parameter to Thread. gh-128555: Add 'context' keyword parameter to Thread. Jan 6, 2025
Doc/library/threading.rst Outdated Show resolved Hide resolved
Doc/library/threading.rst Outdated Show resolved Hide resolved
nascheme and others added 2 commits January 6, 2025 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants