Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Help us test free-threaded Python without the GIL
Hugo van Kemenade
Hugo van Kemenade

Posted on • Edited on

     

Help us test free-threaded Python without the GIL

Moved tohttps://hugovk.dev/blog/2023/help-us-test-free-threaded-python-without-the-gil/

Python 3.13 isdue out in October 2024 and work is underway to implementexperimental support forPEP 703 - Making the Global Interpreter Lock Optional in CPython.

See alsoFree-threaded CPython in "What's New in Python 3.13?"

As the Steering Council noted in theiracceptance of the PEP, to succeed it's important to have community support.

Projects will need to test their code with free-threaded (aka "nogil" butdon't call it that!) Python builds to help us find bugs in CPython, and to check their code is compatible.

Here's some ways to test.

  • Official installers
  • Build it yourself
  • deadsnakes
  • GitHub Actions?
  • Fedora

Official installers

The official macOS (starting in beta 2) and Windows installers has an option to install the free-threading binaries, which also installspython3.13t alongside the regularpython3.13:

Build it yourself

Check out the CPython Git repo andbuild yourself using the--disable-gil configuration flag.

For example, on macOS I run:

$GDBM_CFLAGS="-I$(brew--prefix gdbm)/include"\  GDBM_LIBS="-L$(brew--prefix gdbm)/lib-lgdbm"\  ./configure --config-cache \              --with-system-libmpdec \              --with-openssl="$(brew--prefix openssl@3.0)"\              --disable-gil && make -s -j10$./python.exe-c"import sysconfig; print(sysconfig.get_config_var('Py_GIL_DISABLED'))"1$./python.exe-c"import sys; print(sys._is_gil_enabled())"False
Enter fullscreen modeExit fullscreen mode

More details in the devguide:

GitHub Actions: deadsnakes/action

We can use thedeadsnakes/action to test Ubuntu.

For example:

on:[push,pull_request,workflow_dispatch]jobs:build:runs-on:ubuntu-lateststrategy:matrix:python-version:[3.12,3.13-dev]name:mainsteps:-uses:actions/checkout@v4-uses:actions/setup-python@v5if:"!endsWith(matrix.python-version,'-dev')"with:python-version:${{ matrix.python-version }}-uses:deadsnakes/action@v3.1.0if:endsWith(matrix.python-version, '-dev')with:python-version:${{ matrix.python-version }}nogil:true-run:|python --version --versionwhich pythonpython -c "import sysconfig; print(sysconfig.get_config_var('Py_GIL_DISABLED'))"python -c "import sys; print(sys._is_gil_enabled())"
Enter fullscreen modeExit fullscreen mode

GitHub Actions: actions/setup-python

I've asked for support atactions/setup-python#771, they're looking into it 🤞

In the meantime, give it an upvote, and use deadsnakes/action ⤴️

deadsnakes: PPA

The deadsnakes project provides Personal Package Archives of Python packaged for Ubuntu, included free-threaded builds.

For example,python3.13-nogil under "python3.13 - 3.13.0~a2-1+focal1" and "python3.13 - 3.13.0~a2-1+jammy1" athttps://launchpad.net/~deadsnakes/+archive/ubuntu/ppa/+packages

Fedora

Fedora uses thepython3.13t executable nameas decided by the Steering Council:

$# Install$sudodnfinstallpython3.13-freethreading$# Run$/usr/bin/python3.13t-c"import sysconfig; print(sysconfig.get_config_var('Py_GIL_DISABLED'))"1$/usr/bin/python3.13t-c"import sys; print(sys._is_gil_enabled())"False
Enter fullscreen modeExit fullscreen mode

How to check your build

To confirm if you're using a free-threaded build, check the double--version option (starting in beta 2):

$# Regular build$python3.13--version--versionPython 3.13.0b2 (v3.13.0b2:3a83b172af, Jun  5 2024, 12:50:24) [Clang 15.0.0 (clang-1500.3.9.4)]$# Free-threaded build$python3.13t--version--versionPython 3.13.0b2 experimental free-threading build (v3.13.0b2:3a83b172af, Jun  5 2024, 12:57:31) [Clang 15.0.0 (clang-1500.3.9.4)]
Enter fullscreen modeExit fullscreen mode

Or thePy_GIL_DISABLED macro:

$# Regular build$python3.13-c"import sysconfig; print(sysconfig.get_config_var('Py_GIL_DISABLED'))"0$# Free-threaded build$python3.13t-c"import sysconfig; print(sysconfig.get_config_var('Py_GIL_DISABLED'))"1
Enter fullscreen modeExit fullscreen mode

By default, the GIL is disabled for free-threaded builds, and can be re-enabled by setting thePYTHON_GIL environment variable to1 or running Python with-X gil 1. You can check withsys._is_gil_enabled():

$# Regular build: GIL is always enabled$python3.13-c"import sys; print(sys._is_gil_enabled())"True$# Free-threaded build: GIL is disabled by default$python3.13t-c"import sys; print(sys._is_gil_enabled())"False$# Free-threaded build: re-enable with -X$python3.13t-Xgil=1-c"import sys; print(sys._is_gil_enabled())"True$# Free-threaded build: re-enable with env var$PYTHON_GIL=1 python3.13t-c"import sys; print(sys._is_gil_enabled())"True
Enter fullscreen modeExit fullscreen mode

References:

See also


Header photo: "George Mayerle test chart" byUS National Library of Medicine, withno known copyright restrictions.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Python 3.14 & 3.15 release manager, core developer, PSF Fellow, open-source maintainer, PEP editor, NaNoGenMo organiser
  • Location
    Helsinki, Finland
  • Joined

More fromHugo van Kemenade

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp