The free-threaded build (no GIL)
suggest changeFor most of its history CPython has had a Global Interpreter Lock: only one thread runs Python bytecode at a time, however many cores the machine has (see Global Interpreter Lock and Working around the Global Interpreter Lock GIL). Threads help with I/O-bound work, but CPU-bound Python code doesn't run faster on more threads.
PEP 703 added a second build of CPython that has no GIL. It is called the free-threaded build:
- Python 3.13: added as an experimental build.
- Python 3.14: officially supported (PEP 779). The specializing interpreter now works in this build, so single-threaded code is much less slowed down than it was in 3.13.
It is a separate build, so a normal python3.14 still has the GIL. The free-threaded interpreter is usually installed as python3.14t (t for "threading"), and its C extension modules use the cp314t ABI tag.
This article covers:
- installing the free-threaded build and checking which build you're running
- turning the GIL back on with
PYTHON_GILand-X gil - a CPU-bound benchmark with threads, with and without the GIL
- C extensions, and why importing one can quietly re-enable the GIL
- what thread safety still requires from your own code
Examples were run on WSL Ubuntu with CPython 3.14.8 (both the default and the free-threaded build) and 3.13.16 free-threaded, all installed with uv.
Installing it
Pick whichever of these suits you:
- uv:
uv python install 3.14tinstallspython3.14t.uv run --python 3.14t script.pyruns a script with it, anduv venv --python 3.14tcreates a virtual environment that uses it. - python.org installers for Windows and macOS: choose the option to install the free-threaded binaries. This adds
python3.14t.exe(Windows) orpython3.14t(macOS) next to the normal interpreter. - From source:
./configure --disable-gil.
Both builds can be installed side by side. Packages are not shared between them, because compiled extensions are built separately for each ABI. Use a separate virtual environment for each build:
$ python3.14t -m venv .venv-ft
$ .venv-ft/bin/pip install numpy
Downloading numpy-2.5.4-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (16.8 MB)
Successfully installed numpy-2.5.4
The cp314t in the wheel name means "built for the free-threaded 3.14 ABI". pip only picks wheels with this tag. When a package has no cp314t wheel, pip falls back to the source distribution and tries to compile it.
Which build am I running?
There are two separate questions:
- Was this interpreter built without the GIL? Use
sysconfig.get_config_var("Py_GIL_DISABLED"). - Is the GIL disabled right now? Use
sys._is_gil_enabled()(added in 3.13). A free-threaded build can still run with the GIL turned on, as shown below.
import sys
import sysconfig
print(sys.version)
print("free-threaded build:", bool(sysconfig.get_config_var("Py_GIL_DISABLED")))
print("abiflags:", repr(sys.abiflags))
print("GIL enabled now:", sys._is_gil_enabled())
$ python3.14 check.py
3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]
free-threaded build: False
abiflags: ''
GIL enabled now: True
$ python3.14t check.py
3.14.8 free-threading build (main, Oct 14 2026, 01:04:58) [Clang 23.1.3 ]
free-threaded build: True
abiflags: 't'
GIL enabled now: False
$ python3.13t check.py
3.13.16 experimental free-threading build (main, Oct 14 2026, 01:06:57) [Clang 23.1.3 ]
free-threaded build: True
abiflags: 't'
GIL enabled now: False
The version string also says which build it is: free-threading build in 3.14 and experimental free-threading build in 3.13. python -VV prints it too:
$ python3.14t -VV
Python 3.14.8 free-threading build (main, Oct 14 2026, 01:04:58) [Clang 23.1.3 ]
sys._is_gil_enabled() has a leading underscore and doesn't exist before 3.13. If your code has to run on older versions too, wrap it:
import sys
import sysconfig
def gil_enabled() -> bool:
# sys._is_gil_enabled() only exists on Python 3.13+
is_enabled = getattr(sys, "_is_gil_enabled", None)
return True if is_enabled is None else is_enabled()
def free_threaded_build() -> bool:
return bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
print(sys.version_info[:2], "free-threaded build:", free_threaded_build(),
"GIL enabled:", gil_enabled())
$ py -3.10 portable.py
(3, 10) free-threaded build: False GIL enabled: True
$ py -3.13 portable.py
(3, 13) free-threaded build: False GIL enabled: True
$ python3.14t portable.py
(3, 14) free-threaded build: True GIL enabled: False
Turning the GIL on and off
A free-threaded build can also run with the GIL enabled. You choose with the PYTHON_GIL environment variable or the -X gil option:
PYTHON_GIL=1or-X gil=1: run with the GILPYTHON_GIL=0or-X gil=0: keep the GIL disabled, even when an extension asks for it (see below)
$ PYTHON_GIL=1 python3.14t check.py
3.14.8 free-threading build (main, Oct 14 2026, 01:04:58) [Clang 23.1.3 ]
free-threaded build: True
abiflags: 't'
GIL enabled now: True
$ python3.14t -X gil=1 check.py
...
GIL enabled now: True
sys.flags.gil shows what was asked for on the command line. It is None when you set nothing:
$ python3.14t -c "import sys; print(sys.flags.gil)"
None
$ PYTHON_GIL=1 python3.14t -c "import sys; print(sys.flags.gil)"
1
These options do nothing useful on a regular build, which always has the GIL. Running a free-threaded build with PYTHON_GIL=1 is handy for checking whether a bug comes from missing locks, but it doesn't make it as fast as the default build: you still pay the free-threading overhead.
CPU-bound threads, with and without the GIL
This benchmark starts 1, 2, 4 and 8 threads, and every thread does the same pure-Python work (counting primes below 300,000 by trial division). With perfect parallelism the time stays the same as threads are added. With a GIL it grows with the number of threads, because the threads take turns.
import os
import sys
import threading
import time
def count_primes(limit):
count = 0
for n in range(2, limit):
for d in range(2, int(n**0.5) + 1):
if n % d == 0:
break
else:
count += 1
return count
def run(workers):
# every thread does the same amount of work
threads = [threading.Thread(target=count_primes, args=(300_000,))
for _ in range(workers)]
t0 = time.perf_counter()
for t in threads:
t.start()
for t in threads:
t.join()
return time.perf_counter() - t0
if __name__ == "__main__":
print(f"Python {sys.version.split()[0]}, GIL enabled: {sys._is_gil_enabled()}, "
f"os.cpu_count(): {os.cpu_count()}")
for workers in (1, 2, 4, 8):
secs = min(run(workers) for _ in range(3))
print(f"{workers} thread(s) x same work: {secs:.2f}s")
$ python3.14 bench.py
Python 3.14.8, GIL enabled: True, os.cpu_count(): 24
1 thread(s) x same work: 0.41s
2 thread(s) x same work: 0.77s
4 thread(s) x same work: 1.95s
8 thread(s) x same work: 4.55s
$ python3.14t bench.py
Python 3.14.8, GIL enabled: False, os.cpu_count(): 24
1 thread(s) x same work: 0.39s
2 thread(s) x same work: 0.60s
4 thread(s) x same work: 0.71s
8 thread(s) x same work: 0.78s
$ PYTHON_GIL=1 python3.14t bench.py
Python 3.14.8, GIL enabled: True, os.cpu_count(): 24
1 thread(s) x same work: 0.43s
2 thread(s) x same work: 0.84s
4 thread(s) x same work: 1.66s
8 thread(s) x same work: 2.82s
With the GIL, 8 threads doing 8 units of work take about 8 times as long as one thread doing one unit, so the threads add nothing. Without the GIL, 8 threads finish 8 times the work in about twice the time of one thread. Your numbers will be different: these come from a shared machine where other processes were running (os.cpu_count() reported 24), and runs varied by tens of percent. The overall pattern was the same every time.
The same code works with a concurrent.futures.ThreadPoolExecutor. You don't need a new API: on the free-threaded build, existing threading code can use more than one core.
Single-threaded overhead
Without the GIL, the interpreter needs other ways to keep objects consistent: per-object locks, biased reference counting, and the mimalloc allocator. All of this costs a little time even when only one thread runs. The Python docs give the 3.14 overhead on the pyperformance suite as about 1% on macOS aarch64 and about 8% on x86-64 Linux. What's New in 3.14 says "roughly 5-10%, depending on the platform and C compiler". In 3.13 the overhead was much larger, because the specializing adaptive interpreter was turned off in the free-threaded build.
The free-threaded build also uses more memory, and freed memory can be released a bit later than in the default build. If your program is single-threaded, or its threads mostly wait on I/O, the default build is still the better choice.
C extensions can re-enable the GIL
Pure Python code runs unchanged on the free-threaded build. C extensions are different: a lot of existing C code assumes the GIL protects its global state. An extension module has to declare that it is safe without the GIL. A multi-phase init module does it with the Py_mod_gil slot:
#include <Python.h>
static PyObject *hello(PyObject *self, PyObject *args) {
return PyUnicode_FromString("hello from C");
}
static PyMethodDef methods[] = {
{"hello", hello, METH_NOARGS, NULL},
{NULL, NULL, 0, NULL}
};
static PyModuleDef_Slot slots[] = {
{Py_mod_gil, Py_MOD_GIL_NOT_USED}, /* "I don't need the GIL" */
{0, NULL}
};
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT, "modern", NULL, 0, methods, slots,
};
PyMODINIT_FUNC PyInit_modern(void) { return PyModuleDef_Init(&module); }
(Single-phase init modules call PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED) instead. Cython has a freethreading_compatible directive, and pybind11 and nanobind have options for this too.)
I built that file twice for python3.14t: as modern with the slot, and as legacy without it. Importing legacy turns the GIL back on for the whole process and prints a RuntimeWarning:
import sys
print("before import, GIL enabled:", sys._is_gil_enabled())
import legacy
print(legacy.hello())
print("after import, GIL enabled:", sys._is_gil_enabled())
$ python3.14t use.py
<frozen importlib._bootstrap>:491: RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module 'legacy', which has not declared that it can run safely without the GIL. To override this behavior and keep the GIL disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.
before import, GIL enabled: False
hello from C
after import, GIL enabled: True
The module that declares support doesn't change anything:
$ python3.14t use2.py
hello from C
GIL enabled: False
With PYTHON_GIL=0 the GIL stays off even for legacy. That's only safe if you know the extension doesn't rely on the GIL:
$ PYTHON_GIL=0 python3.14t use.py
before import, GIL enabled: False
hello from C
after import, GIL enabled: False
In practice:
- If your threads don't get faster on
python3.14t, look for this warning first. A single old extension anywhere in your imports puts the GIL back for the whole program. - Add
assert not sys._is_gil_enabled()after your imports in a benchmark or a test, so this can't happen silently. - Most big packages with compiled code (NumPy, for example, as installed above) publish
cp314twheels. For others, check the package's release notes or the community compatibility tracker at py-free-threading.github.io. - From 3.14, when building extensions for the free-threaded build on Windows, the build backend has to define
Py_GIL_DISABLEDitself (the compiler no longer works it out). Use a build backend version that supports 3.14 free-threaded builds.
What thread safety still requires
Removing the GIL doesn't make your code thread-safe. The GIL never made compound operations atomic. It mostly hid races because threads switched rarely. Without it, threads really run at the same time, and races that were rare become common. See Race conditions and Locks and synchronization for the general tools.
x += 1 is not atomic
counter += 1 is a read, an add and a write. Two threads can read the same value, and then one of the increments is lost:
import sys
import threading
counter = 0
def work():
global counter
for _ in range(1_000_000):
counter += 1
threads = [threading.Thread(target=work) for _ in range(4)]
for t in threads:
t.start()
for t in threads:
t.join()
print(f"GIL enabled: {sys._is_gil_enabled()}, counter = {counter:,} (expected 4,000,000)")
$ python3.14 race.py
GIL enabled: True, counter = 4,000,000 (expected 4,000,000)
$ python3.14t race.py
GIL enabled: False, counter = 1,445,947 (expected 4,000,000)
$ python3.14t race.py
GIL enabled: False, counter = 1,496,191 (expected 4,000,000)
With the GIL the result happens to be correct here (CPython only switches threads at certain points in the bytecode), but nothing guarantees it. Without the GIL, most of the increments are lost. The fix is the same in both builds, a lock:
import threading
counter = 0
lock = threading.Lock()
def work():
global counter
for _ in range(1_000_000):
with lock:
counter += 1
threads = [threading.Thread(target=work) for _ in range(4)]
for t in threads:
t.start()
for t in threads:
t.join()
print(f"counter = {counter:,}")
$ python3.14t lockfix.py
counter = 4,000,000
A shared counter behind a lock is also slow, because every increment waits for the lock. It's better to give each thread its own data and combine the results at the end, as count_primes does in the benchmark.
Single operations on built-in containers are safe
In the free-threaded build, list, dict, set and other built-in types have internal per-object locks. A single operation such as list.append, dict[key] = value or set.add won't corrupt the object or crash the interpreter:
import sys
import threading
items = []
seen = {}
def work(tid):
for i in range(100_000):
items.append(i) # one call: internally locked
seen[(tid, i)] = True # one call: internally locked
threads = [threading.Thread(target=work, args=(t,)) for t in range(8)]
for t in threads:
t.start()
for t in threads:
t.join()
print(f"GIL enabled: {sys._is_gil_enabled()}")
print(f"len(items) = {len(items):,}, len(seen) = {len(seen):,}")
$ python3.14 builtins_safe.py
GIL enabled: True
len(items) = 800,000, len(seen) = 800,000
$ python3.14t builtins_safe.py
GIL enabled: False
len(items) = 800,000, len(seen) = 800,000
The Python docs still recommend using threading.Lock and other synchronization primitives rather than relying on these internal locks.
Check-then-act needs a lock
Each operation is safe on its own, but a sequence of them is not. Here 8 threads sell apples until the stock runs out:
import threading
stock = {"apples": 100_000}
sold = [0] * 8 # one slot per thread, so counting sales is not a race
def buy(tid):
while stock["apples"] > 0: # check ...
stock["apples"] -= 1 # ... then act: not atomic as a whole
sold[tid] += 1
threads = [threading.Thread(target=buy, args=(t,)) for t in range(8)]
for t in threads:
t.start()
for t in threads:
t.join()
print(f"apples left: {stock['apples']}, sold: {sum(sold):,}")
$ python3.14t checkthen.py
apples left: 0, sold: 326,177
$ python3.14t checkthen.py
apples left: -1, sold: 304,509
$ python3.14 checkthen.py
apples left: 0, sold: 100,000
Without the GIL, the program sold more than three times the stock, and on one run the stock went negative. stock["apples"] -= 1 reads the value and writes it back in two separate dictionary operations, and the check and the update are separate steps too. Put the whole check-and-update under one threading.Lock, or use a queue.Queue, which handles its own locking.
Other rules from the docs
- Don't use the same iterator from several threads at once. Threads may see duplicate or missing elements. Give each thread its own iterator, or pass work through a
queue.Queue. - Reading
frame.f_localsof a frame that is running in another thread is not safe and may crash the interpreter. - Some objects are immortal in the free-threaded build (in 3.14, code constants and strings interned with
sys.intern()). Their reference count never changes and they are never freed, sosys.getrefcount()gives odd values for them.
Context variables are inherited by threads
Two flags default to true in the free-threaded build and to false in the default build (they're new in 3.14, and both builds can set them with -X):
thread_inherit_context: a newthreading.Threadstarts with a copy of the caller'scontextvarscontext instead of an empty one.context_aware_warnings:warnings.catch_warnings()uses a context variable instead of changing the global filter list, which wasn't thread-safe.
import contextvars
import sys
import threading
print("thread_inherit_context:", sys.flags.thread_inherit_context)
print("context_aware_warnings:", sys.flags.context_aware_warnings)
request_id = contextvars.ContextVar("request_id", default="-")
request_id.set("req-42")
t = threading.Thread(target=lambda: print("in thread:", request_id.get()))
t.start()
t.join()
$ python3.14 flags.py
thread_inherit_context: 0
context_aware_warnings: 0
in thread: -
$ python3.14t flags.py
thread_inherit_context: 1
context_aware_warnings: 1
in thread: req-42
This also affects decimal.localcontext() and anything else built on context variables. If code depends on threads starting with a clean context, it behaves differently on the two builds.
When to use it
The free-threaded build is worth trying when:
- the work is CPU-bound pure Python that you can split across threads
- the threads need to share large in-memory data structures, which is expensive with Multiprocessing.Pool because data is pickled between processes
- every extension you import declares free-threading support (check that
sys._is_gil_enabled()staysFalse)
Stay with the default build when the program is single-threaded or I/O-bound, or when it depends on extensions that aren't ready yet. Other ways around the GIL are processes, subinterpreters, and releasing the GIL in native code (Cython nogil).
Summary
| Task | How |
|---|---|
| install | uv python install 3.14t, or the free-threaded option in the python.org installer |
| run a script | python3.14t script.py or uv run --python 3.14t script.py |
| is this a free-threaded build? | sysconfig.get_config_var("Py_GIL_DISABLED") is 1 |
| is the GIL off right now? | sys._is_gil_enabled() is False (3.13+) |
| force the GIL on | PYTHON_GIL=1 or -X gil=1 |
| keep the GIL off despite old extensions | PYTHON_GIL=0 or -X gil=0 (at your own risk) |
| mark a C extension as safe | {Py_mod_gil, Py_MOD_GIL_NOT_USED} slot |
| wheel tag | cp313t, cp314t |
Things to remember:
- 3.13t is experimental. 3.14t is officially supported, and its single-threaded overhead is much lower (about 5-10%).
- Importing one extension that doesn't declare support turns the GIL back on for the whole process. Watch for the
RuntimeWarning. - Built-in containers won't be corrupted by concurrent use, but
x += 1and check-then-act sequences still need a lock.