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-128555: Add 'context' keyword parameter to Thread. by nascheme · Pull Request #128209 · python/cpython

Conversation

@nascheme

@nascheme nascheme commented Dec 24, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Edit: this PR has been revised to make the behavior dependent on the build of Python used. The free-threaded build defaults to inheriting the context of the caller of Thread.start(). The default behavior can be overridden by an -X var or by an environment variable. In the long term I think the behavior should be consistent between builds, both inheriting by default. The flag gives users of the default build an opt-in setting.

  • Add the sys.flags.thread_inherit_context flag.

  • This flag is set to true by default on the free-threaded build and false otherwise. If the flag is true, starting a new thread using threading.Thread will, by default, use a copy of the contextvars.Context from the caller of threading.Thread.start rather than using an empty context.

  • Add the -X thread_inherit_context command-line option and PYTHON_THREAD_INHERIT_CONTEXT environment variable, which set the sys.flags.thread_inherit_context flag.

  • Add the context keyword parameter to threading.Thread. It can be used to explicitly pass a context value to be used by a new thread.

  • Make the _contextvars module built-in.

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.

Making new threads inherit context 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/

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

Sorry, something went wrong.

Uh oh!

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

asvetlov reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@nascheme nascheme mentioned this pull request Dec 27, 2024
gh-128384: Add thread-safe context manager to "warnings" module #128300
Closed
@nascheme
nascheme force-pushed the thread_inherit_context branch from 708127d to 808fa72 Compare December 28, 2024 21:05
kumaraditya303
kumaraditya303 reviewed Jan 2, 2025
View reviewed changes
Comment thread Doc/library/threading.rst Resolved

Uh oh!

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

@nascheme nascheme mentioned this pull request Jan 3, 2025
gh-128384: Use contextvar for catch_warnings() #128463
Closed
@nascheme

nascheme commented Jan 3, 2025

Copy link
Copy Markdown
Member Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

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

gpshead
gpshead reviewed Jan 3, 2025
View reviewed changes
Comment thread Doc/library/threading.rst Outdated
Use the *target* name if *name* argument is omitted.

.. versionchanged:: 3.14
Added the *context* parameter. Previously threads always ran with an empty

@gpshead gpshead Jan 3, 2025

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.

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?

Sorry, something went wrong.

Uh oh!

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

All reactions

@nascheme nascheme Feb 11, 2025

Copy link
Copy Markdown
Member 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 asked Sam what he thought about making this behavior enabled by default on free-threaded builds and he's okay with the idea. I've updated this PR to take that approach. I think we can wait a couple of releases to decide what to do about the default build. If it seems there is little breakage, we can change the default there too after a suitable period.

Sorry, something went wrong.

Uh oh!

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

All reactions
gpshead
gpshead reviewed Jan 3, 2025
View reviewed changes
Comment thread Doc/library/threading.rst Resolved

Uh oh!

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

gpshead
gpshead reviewed Jan 3, 2025
View reviewed changes
Comment thread Lib/threading.py Resolved

Uh oh!

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

@nascheme nascheme changed the title Add 'context' keyword parameter to Thread. gh-128555: Add 'context' keyword parameter to Thread. Jan 6, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Jan 6, 2025
New threads should inherit the contextvars.Context of their creator #128555
Closed
kumaraditya303
kumaraditya303 reviewed Jan 7, 2025
View reviewed changes
Comment thread Doc/library/threading.rst Resolved

Uh oh!

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

Comment thread Doc/library/threading.rst Resolved

Uh oh!

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

nascheme added 2 commits February 6, 2025 12:53
@nascheme
Make _contextvars a builtin module.
ba99f2e
@nascheme
Add 'context' parameter to Thread.
6d00c2a
* Add ``sys.flags.inherit_context``.
* Add ``-X inherit_context`` and :envvar:`PYTHON_INHERIT_CONTEXT`
@nascheme
nascheme force-pushed the thread_inherit_context branch from 19d637a to 6d00c2a Compare February 6, 2025 22:29
nascheme added 2 commits February 6, 2025 16:10
@nascheme
Tweak blurb markup.
a868fe9
@nascheme
Doc markup fix.
16fa2c3
@nascheme nascheme mentioned this pull request Feb 11, 2025
gh-128384: Use a context variable for warnings.catch_warnings #130010
Merged
@nascheme
nascheme marked this pull request as ready for review February 11, 2025 18:28
@nascheme
nascheme requested review from a team, 1st1, corona10, ericsnowcurrently and erlend-aasland as code owners February 11, 2025 18:28
@bedevere-app bedevere-app Bot added the awaiting core review label Feb 11, 2025
nascheme added 5 commits February 11, 2025 15:23
@nascheme
Rename flag to thread_inherit_context.
09e72b8
@nascheme
Merge branch 'main' into thread_inherit_context
75f1b38
@nascheme
Regenerate 'configure' script.
872920d
@nascheme
Add comment about why 'context' is passed in test.
2c48fc5
@nascheme
Revise "decimal' docs, adding note about flag.
53eb72d
@nascheme

nascheme commented Apr 9, 2025

Copy link
Copy Markdown
Member Author

Merged along with gh-130010.

All reactions

Sorry, something went wrong.

Uh oh!

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

@nascheme nascheme closed this Apr 9, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Jul 12, 2026
docs(otel-python): refresh released SDK guidance ollygarden/opentelemetry-agent-skills#128
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gpshead gpshead gpshead left review comments
@kumaraditya303 kumaraditya303 kumaraditya303 left review comments
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@1st1 1st1 Awaiting requested review from 1st1 1st1 is a code owner
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner
@corona10 corona10 Awaiting requested review from corona10 corona10 is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

awaiting core review

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.

3 participants

@nascheme @gpshead @kumaraditya303
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.