Skip to content

gh-116167: Allow disabling the GIL with PYTHON_GIL=0 or -X gil=0 - #116338

Merged
colesbury merged 11 commits into
python:mainfrom
swtaarrs:cpython-disable-gil
Mar 11, 2024
Merged

gh-116167: Allow disabling the GIL with PYTHON_GIL=0 or -X gil=0#116338
colesbury merged 11 commits into
python:mainfrom
swtaarrs:cpython-disable-gil

Conversation

@swtaarrs

@swtaarrsswtaarrs commented Mar 4, 2024

Copy link
Copy Markdown
Member

In free-threaded builds, running with PYTHON_GIL=0 or -X gil=0 will now disable the GIL. #116322 and #116329 track follow-up work to re-enable the GIL when loading an incompatible extension, and to disable the GIL by default, respectively.

In order to support re-enabling the GIL at runtime, all GIL-related data structures are initialized as usual, and disabling the GIL simply sets a flag at startup that causes take_gil() and drop_gil() to return early.

With PYTHON_GIL=0 set, I spot-checked a few tests and small programs that don't use threads. They all seem to run fine, and very basic threaded programs work, sometimes. Trying to run the full test suite crashes pretty quickly, in test_asyncio.

@swtaarrs

Copy link
Copy Markdown
MemberAuthor

!buildbot nogil

@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @swtaarrs for commit ac24233 🤖

The command will test the builders whose names match following regular expression: nogil

The builders matched are:

  • x86-64 MacOS Intel ASAN NoGIL PR
  • x86-64 MacOS Intel NoGIL PR
  • ARM64 MacOS M1 Refleaks NoGIL PR
  • ARM64 MacOS M1 NoGIL PR
  • AMD64 Ubuntu NoGIL Refleaks PR
  • AMD64 Ubuntu NoGIL PR
  • AMD64 Windows Server 2022 NoGIL PR

In free-threaded builds, running with `PYTHON_GIL=0` will now disable the
GIL. Follow-up issues track work to re-enable the GIL when loading an
incompatible extension, and to disable the GIL by default.
In order to support re-enabling the GIL at runtime, all GIL-related data
structures are initialized as usual, and disabling the GIL simply sets a flag
that causes `take_gil()` and `drop_gil()` to return early.
With `PYTHON_GIL=0` set, I spot-checked a few tests and small programs that
don't use threads. They all seem to run fine, and very basic threaded programs
work, sometimes. Trying to run the full test suite crashes pretty quickly, in
`test_asyncio`.
@swtaarrs
swtaarrsforce-pushed the cpython-disable-gil branch from ac24233 to 9e03999CompareMarch 5, 2024 20:38
@swtaarrs

Copy link
Copy Markdown
MemberAuthor

!buildbot nogil

@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @swtaarrs for commit 9e03999 🤖

The command will test the builders whose names match following regular expression: nogil

The builders matched are:

  • x86-64 MacOS Intel ASAN NoGIL PR
  • ARM64 MacOS M1 Refleaks NoGIL PR
  • AMD64 Windows Server 2022 NoGIL PR
  • AMD64 Ubuntu NoGIL PR
  • x86-64 MacOS Intel NoGIL PR
  • AMD64 Ubuntu NoGIL Refleaks PR
  • ARM64 MacOS M1 NoGIL PR

@corona10corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good to add test case in this file too.

deftest_int_max_str_digits(self):

@corona10

corona10 commented Mar 5, 2024

Copy link
Copy Markdown
Member

Ah ignore above comments, this PR only referencing the environment variable so not proper for this case.

You can just add the testcase for the environment variable, still worth to adding it.

@corona10

Copy link
Copy Markdown
Member

At not for this PR, since this feature is not officially announced, but in some moment, you should write about the environment variable at https://github.com/python/cpython/blob/72714c0266ce6d39c7c7fb63f617573b8f5a3cb2/Doc/using/cmdline.rst to get user feedback.

@swtaarrs

Copy link
Copy Markdown
MemberAuthor

!buildbot nogil

@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @swtaarrs for commit 4f43a87 🤖

The command will test the builders whose names match following regular expression: nogil

The builders matched are:

  • x86-64 MacOS Intel ASAN NoGIL PR
  • ARM64 MacOS M1 NoGIL PR
  • AMD64 Ubuntu NoGIL PR
  • x86-64 MacOS Intel NoGIL PR
  • ARM64 MacOS M1 Refleaks NoGIL PR
  • AMD64 Ubuntu NoGIL Refleaks PR
  • AMD64 Windows Server 2022 NoGIL PR

@swtaarrs
swtaarrs marked this pull request as ready for review March 5, 2024 22:56
@swtaarrs
swtaarrs requested a review from vstinnerMarch 5, 2024 23:16
@swtaarrs

Copy link
Copy Markdown
MemberAuthor

you should write about the environment variable ... to get user feedback

What would be a good time to do that? Maybe once the GIL is disabled by default, so the description doesn't have to change?

Comment threadPython/initconfig.c Outdated
@colesbury

colesbury commented Mar 6, 2024

Copy link
Copy Markdown
Contributor

I would document the environment variable now, when it's added. For reference, we already document the --disable-gil configure flag.

I don't think it needs to be extensive, but should cover:

  • Only available in free-threaded builds
  • 0 forces the GIL to be disabled
  • 1 forces the GIL to be enabled
  • New in version 3.13

The behavior when then environment variable is not set is what's going to change in the future. We can document that later.

@swtaarrs

Copy link
Copy Markdown
MemberAuthor

After discussion in Discord, I'm also going to add a -X option to go with the environment variable.

@swtaarrs
swtaarrs marked this pull request as draft March 7, 2024 14:31
@swtaarrsswtaarrs changed the title gh-116167: Allow disabling the GIL with PYTHON_GIL=0gh-116167: Allow disabling the GIL with PYTHON_GIL=0 or -X gil=0Mar 7, 2024
@swtaarrs
swtaarrs marked this pull request as ready for review March 7, 2024 22:29
@swtaarrs
swtaarrs requested a review from gpshead as a code ownerMarch 7, 2024 22:29

@ericsnowcurrentlyericsnowcurrently left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

I've left one minor comment about an unclear test. It really isn't a blocker, as the test is passing, but it would still be nice to have a response.

Comment threadLib/test/_test_embed_set_config.py Outdated
@colesbury
colesbury merged commit 2731913 into python:mainMar 11, 2024
@vstinner

Copy link
Copy Markdown
Member

This is both exciting and very scary. Congrats @swtaarrs and @colesbury :-)

@alikud

This comment was marked as off-topic.

@edpyt

This comment was marked as off-topic.

@fractalvision

This comment was marked as off-topic.

@pythonpython locked as spam and limited conversation to collaborators Mar 11, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@swtaarrs@bedevere-bot@corona10@colesbury@vstinner@alikud@edpyt@fractalvision@ericsnowcurrently@erlend-aasland