Sub topic 14
Where the GIL is going: free-threaded builds and per-interpreter GIL
This chapter assumes you know what the GIL protects, why CPU-bound threads don't run in parallel under it, and how ProcessPoolExecutor works around that. It describes the direction CPython is taking. Details here have changed between releases, so check the documentation for your version.
Two separate projects
Two efforts reduce the GIL's limits. They are independent and solve different problems.
| Approach | Idea | Status |
|---|---|---|
| Free-threaded build | Remove the GIL. Threads in one interpreter run Python code in parallel. | Experimental in 3.13. Officially supported, though still optional, from 3.14. |
| Per-interpreter GIL | Keep a GIL, but give each sub-interpreter in a process its own. | Isolation in 3.12 (C API only). A Python-level `concurrent.interpreters` module arrived in 3.14. |
The free-threaded build
A free-threaded build is a separate interpreter binary, often installed as python3.13t or python3.14t. It replaces the GIL with finer-grained mechanisms inside the interpreter, such as per-object locks and changes to reference counting. Threads can then run pure-Python CPU-bound code on several cores at once.
Three points matter in practice:
- Single-thread cost. The free-threaded build can run single-threaded code more slowly than the standard build. Measure your own workload.
- Extension modules. C extensions must be built and marked as compatible. If you import one that isn't, CPython may re-enable the GIL for the whole process.
- Your code still needs locks. The GIL never made
counter += 1atomic. Without it, races that were rare become more likely. Usethreading.Lockandqueue.Queueexactly as before.
You can check which mode you are in at runtime:
import sys
# sys._is_gil_enabled() exists in Python 3.13 and later; older versions always have a GIL.
gil_enabled = getattr(sys, "_is_gil_enabled", lambda: True)()
print("GIL enabled:", gil_enabled)This prints False only on a free-threaded build that has not re-enabled the GIL. On a free-threaded build you can also force the setting with the PYTHON_GIL environment variable (0 or 1) or the -X gil option. Use this to compare timings on the same binary.
Per-interpreter GIL
A process can host several sub-interpreters. Since Python 3.12, each one can have its own GIL, so they can run Python code in parallel inside a single process. Each interpreter has its own modules and objects. You cannot share a plain list between them. You pass data across, much as you do between processes, but without the cost of starting a full OS process.
Python 3.14 exposes this through the concurrent.interpreters module and an InterpreterPoolExecutor in concurrent.futures. These APIs are young. Treat their details as likely to change, and prefer ProcessPoolExecutor for production code until they settle.
What to do today
- Keep using threads for I/O-bound work and processes for CPU-bound work. This is correct on every standard build.
- Write thread-safe code now, with locks and queues. It will behave correctly on both kinds of build.
- If you want to try a free-threaded build, test it with your dependencies and time the result against a process pool.
- Don't write code that depends on a particular GIL behavior, such as an operation seeming atomic.
Exercise
Your team's script uses a ThreadPoolExecutor for a CPU-bound job and gains nothing. A colleague suggests running it on a free-threaded build. Name two things you should check before expecting a speedup.
Show answer
First, confirm the GIL is actually off at runtime, for example with sys.isgil_enabled(), because importing an extension module that isn't marked compatible can re-enable it. Second, measure the free-threaded result against the original and against a ProcessPoolExecutor. Single-threaded overhead and lock contention can cancel the gain. Also check that the shared data is protected by locks.