Sub topic 5
Turning the GIL on and off at runtime
This chapter assumes you can run Python scripts from a shell and have a free-threaded interpreter such as python3.13t installed. The previous chapter showed how to detect one. Here you learn how to control whether the GIL is active when that interpreter starts.
Two separate questions
A free-threaded build and an active GIL are different facts about a running program:
- Build type is fixed when the interpreter is compiled. A standard build always has the GIL.
- GIL state is a runtime setting that only a free-threaded build can change.
Because of this, check both values. Do not assume that a free-threaded build is running without the GIL.
import sys
import sysconfig
# 1 on a free-threaded build, 0 or None on a standard build
print("Free-threaded build:", sysconfig.get_config_var("Py_GIL_DISABLED"))
# Whether the GIL is active in this running interpreter
print("GIL enabled:", sys._is_gil_enabled())The two controls
Both controls are read once, at interpreter startup:
- The environment variable
PYTHON_GIL. - The command-line option
-X gil.
Each accepts 0 to disable the GIL and 1 to enable it.
# Run with the GIL disabled (free-threaded build, e.g. python3.13t)
PYTHON_GIL=0 python3.13t script.py
# Equivalent command-line option
python3.13t -X gil=0 script.py
# Force the GIL on, even on a free-threaded build
PYTHON_GIL=1 python3.13t script.pyIf you set both, the command-line option takes precedence over the environment variable. Setting neither gives you the build's default behavior.
The default and automatic re-enabling
On a free-threaded build with no setting, the GIL starts disabled. The interpreter can turn it on later. If your program imports a C extension that has not declared free-threading support, CPython re-enables the GIL and prints a RuntimeWarning. This protects code that was written assuming the GIL exists.
Setting PYTHON_GIL=0 overrides that protection. The extension is loaded and the GIL stays off, even though the extension may not be safe without it. Only do this when you have tested the extension or know the authors support free-threading.
Checking the state while running
Because the GIL can be re-enabled after startup, check sys._is_gil_enabled() after your imports, not only at the top of the script. A common pattern is to assert it in a benchmark or test:
import sys
import numpy # import everything your program needs first
if sys._is_gil_enabled():
raise SystemExit("GIL is on; parallel results will be misleading")
print("Running without the GIL")The function is underscore-prefixed, so it is not a stable public API. Wrap it in getattr(sys, "_is_gil_enabled", lambda: True)() if your code must also run on older versions.
Common mistakes
- Timing a script on
python3.13tand seeing no speedup. An unsupported import probably re-enabled the GIL. Look for theRuntimeWarningin stderr. - Setting
PYTHON_GIL=0in a shell profile. Every free-threaded run then ignores extension safety checks. - Expecting to switch the GIL from inside a running program. The switch is chosen at startup, and only the import-time re-enabling changes it afterward.
Exercise
You run python3.13t script.py and the script's CPU-bound threads show no speedup. The output includes a RuntimeWarning after an import. What most likely happened, and how can you confirm it and test further?
Show answer
The imported C extension did not declare free-threading support, so the interpreter re-enabled the GIL. Confirm by printing sys._is_gil_enabled() after the imports. It returns True. To test further, run with PYTHON_GIL=0 or -X gil=0 to keep the GIL off, but only for a quick experiment, because the extension may not be safe without the GIL.