Programming Search⌘K

Featureadvanced

Free-threaded Python

Also called free threading, nogil, GIL-free Python, python3.14t, python3.13t, PEP 703, PEP 779, Py_GIL_DISABLED, PYTHON_GIL, --disable-gil.

The free-threaded build of CPython runs without the global interpreter lock, so threads run Python code in parallel. It came in 3.13 as an experiment and is a supported, optional build from 3.14.

Language
Python
Since
3.13
Proposals
pep-703, pep-779

Synopsis

import sys
import sysconfig

print(sysconfig.get_config_var("Py_GIL_DISABLED"))
print("free-threading build" in sys.version)
print(sys._is_gil_enabled())
Output
0
False
True

This output comes from the default build. On a free-threaded build such as python3.14t, the first line is 1, the second is True, and the GIL is off unless something turns it on again.

Semantics

r1. CPython "has support for a build of Python called free threading where the global interpreter lock (GIL) is disabled." Such a build runs "threads in parallel on available CPU cores."1

r2. The free-threaded build is a separate executable. In 3.13 it is "usually called python3.13t or python3.13t.exe", and a source build uses the --disable-gil option.2

r3. The configuration variable Py_GIL_DISABLED tells the build: "If the variable is set to 1, then the build supports free threading." sys._is_gil_enabled() tells whether the GIL "is actually disabled in the running process."1 Example: which-build.

r4. A free-threaded build can run with the GIL on. The environment variable PYTHON_GIL or the option -X gil turns it on. "The GIL may also automatically be enabled when importing a C-API extension module that is not explicitly marked as supporting free threading."1

r5. "Built-in types like dict, list, and set use internal locks to protect against concurrent modifications in ways that behave similarly to the GIL." The guide still recommends "threading.Lock or other synchronization primitives instead of relying on the internal locks of built-in types".1 Example: portable-lock.

r6. The Thread Safety Guarantees page lists the rules for each type. For example, lst.append(x) and lst.pop() are atomic, but "Operations that involve multiple accesses, as well as iteration, are never atomic."3

r7. "It is generally not thread-safe to access the same iterator object from multiple threads concurrently, and threads may see duplicate or missing elements."4

r8. The build changes some defaults. In it, "the flag thread_inherit_context is set to true by default", so a new threading.Thread starts with a copy of the context of the caller of start(). In the default build, a thread starts with an empty context.4 Example: inherit-context.

r9. The build "has additional overhead when executing Python code compared to the default GIL-enabled build", and it "will typically use more memory".4

Examples

import threading

totals = {"hits": 0}
lock = threading.Lock()

def hit(times):
    for _ in range(times):
        with lock:
            totals["hits"] += 1

threads = [threading.Thread(target=hit, args=(5_000,)) for _ in range(4)]
for t in threads:
    t.start()
for t in threads:
    t.join()
print(totals)
Output
{'hits': 20000}

totals["hits"] += 1 is a read, an add and a write, so the internal lock of the dict does not make it atomic (rule r6). With the explicit lock, the program prints the same total on both builds.

import contextvars
import sys
import threading

user = contextvars.ContextVar("user", default="nobody")
user.set("alice")

def show():
    print("in the thread:", user.get())

print("inherit:", bool(sys.flags.thread_inherit_context))
t = threading.Thread(target=show)
t.start()
t.join()

t = threading.Thread(target=show, context=contextvars.copy_context())
t.start()
t.join()
Output
inherit: False
in the thread: nobody
in the thread: alice

The default build does not copy the context, so the first thread sees the default value. A thread that gets context= sees the value on both builds (rule r8).

Versions

Since 3.13

The free-threaded build is available as an experiment, "not enabled by default", with "a substantial single-threaded performance hit."2

Since 3.14

The implementation of PEP 703 is finished, and the specializing adaptive interpreter works in free-threaded mode. "The performance penalty on single-threaded code in free-threaded mode is now roughly 5-10%".5 PEP 779, for 3.14, sets the criteria for the next phase: "Phase II would make the free-threaded build officially supported but still optional, and phase III would make the free-threaded build the default."6

Pitfalls

An extension module that does not declare support turns the GIL on again at import (rule r4). A program then runs with the GIL, and threads give no speedup. Check sys._is_gil_enabled() after the imports.

Code that was safe only because of the GIL can fail. Two threads that share an iterator can skip or repeat items (rule r7). A check followed by an action, such as if key in d: del d[key], is not atomic; the guarantees page suggests d.pop(key, None) instead.3

A thread in the free-threaded build sees the context variables of its parent, and a thread in the default build does not (rule r8). Pass context= to Thread when the code must behave the same on both builds.

Comparison

In Go, Java and C#, threads always run in parallel, and the programmer writes the locks. Free-threaded Python moves CPython toward that model. Its built-in types keep internal locks, so a data race corrupts no list or dict, but the order of the program's own steps is still the job of its locks (rules r5 and r6).

References

  1. Python support for free threading, Python 3.15 documentation, the introduction; Identifying free-threaded Python; The global interpreter lock in free-threaded Python; Thread safety
  2. What’s New In Python 3.13, Free-threaded CPython
  3. Thread Safety Guarantees, Python 3.15 documentation, Thread safety for list objects; Thread safety for dict objects
  4. Python support for free threading, Python 3.15 documentation, Known limitations; Behavioral changes
  5. What’s New In Python 3.14, Free-threaded mode improvements
  6. PEP 779: Criteria for supported status for free-threaded Python, Abstract; Motivation

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source