
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
:
- python/cpython#120098
- https://docs.python.org/3.13/using/windows.html#installing-free-threaded-binaries
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
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())"
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
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)]
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
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
References:
- https://docs.python.org/3.13/using/configure.html#cmdoption-disable-gil
- https://docs.python.org/3.13/using/cmdline.html#envvar-PYTHON_GIL
- https://docs.python.org/3.13/using/cmdline.html#cmdoption-X
See also
- Help test Python 3.13!
- C API Extension Support for Free Threading
- Free-threaded CPython is ready to experiment with!
- py-free-threading
- Improving Ecosystem Compatibility with Free-Threaded Python
Header photo: "George Mayerle test chart" byUS National Library of Medicine, withno known copyright restrictions.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse