Lesson · Concurrency Fundamentals
The GIL
96 min readThe GIL
The traditional CPython GIL limits simultaneous execution of Python bytecode within one interpreter, but Python 3.14 officially supports a free-threaded build. Neither model makes shared mutable state automatically safe.
Learning goals
- Explain what the GIL actually guarantees in a traditional CPython build, and what it does not.
- Say why CPU-heavy threads do not speed up under the GIL while I/O-heavy threads still overlap their waits.
- Check which build is running with
sys._is_gil_enabled()under its guard and interpret the answer for either build. - Describe what changes, and what does not, when a free-threaded build removes the lock.
- List the compatibility questions a free-threaded deployment adds for native extension modules.
Mental model
The GIL is a statement about how one interpreter executes bytecode, not a statement about the safety of your data: it can switch threads between bytecodes, so compound operations stay unprotected either way. The free-threaded build changes how much can run at once; it does not change which operations need coordination.
One core doing both jobs, then two cores
15 px per millisecond on the shared axis
Mental model
the same mechanism as a compact list
traditional CPython build one thread executes bytecode at a time ↓ still true scheduling happens between bytecodes compound operations interleave counter += 1 can still lose updates ↓ free-threaded build threads run bytecode in parallel more interleaving, not less synchronization stays a design obligation
Version note
Free-threaded CPython, introduced experimentally in 3.13, is officially supported in the Python 3.14 series. It remains a distinct build/runtime mode.
Core idea
The GIL is an implementation choice about one interpreter, and the free-threaded build is a second implementation of the same language, so neither is a correctness rule you can lean on. What both builds share is the lesson's real subject: shared mutable state needs coordination, and the GIL only changes how often interleaving can happen, not whether it can. Read every claim about the GIL as a statement about a particular build, then ask what the program's invariants actually require.
What the Global Interpreter Lock is
In a traditional CPython build, the GIL allows only one thread at a time to execute Python bytecode in a given interpreter. It simplifies parts of CPython's object model but limits CPU-bound Python-thread parallelism.
What it does not mean
In The GIL, What it does not mean names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
CPU-bound threading limitations in CPython
In The GIL, CPU-bound threading limitations in CPython names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
I/O-bound threading
In The GIL, I/O-bound threading names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Native extensions overview
Native extensions overview introduces the available mechanisms and their intended roles without requiring every low-level detail. Use the comparison points in this lesson to decide which mechanism deserves deeper study for a specific workload.
Language guarantees vs implementation details
This comparison separates Language guarantees from implementation details in the context of The GIL. Focus on ownership, mutability, cost, failure behavior, and API intent rather than treating the two choices as interchangeable.
Traditional GIL-enabled CPython vs free-threaded CPython builds
Python 3.14 officially supports a free-threaded CPython build that can run Python threads in parallel on multiple cores. It is an optional runtime mode with compatibility and synchronization implications.
Why free-threading does not remove the need for synchronization
Removing the GIL permits more simultaneous execution, which makes shared-state races more—not less—important. Correct programs still need locks, queues, immutability, or other coordination.
Extension compatibility considerations
An editor should make the active interpreter visible and provide syntax, linting, debugging, and terminal access without hiding how Python itself runs. Keep environment selection explicit.
Python 3.14 example: detect the GIL mode
Do not branch application correctness on the GIL. Synchronize shared invariants correctly in both runtime modes.
import sys if hasattr(sys, "_is_gil_enabled"): print("GIL enabled:", sys._is_gil_enabled()) else: print("This interpreter predates the Python 3.14 inspection API used here.")
Playable lesson
Lab: GIL/free-threading comparison
Switch between traditional GIL-enabled CPython and free-threaded CPython. Show that synchronization remains necessary for shared invariants.
The lab should ask for a prediction before each state transition. Include a reset action and at least one intentionally misleading case.
GIL timeline
traditional build, GIL enabled
Thread timeline (same clock for both rows)
20 ms elapsed
- T1 0 to 10 ms: running
- T2 0 to 10 ms: waiting for the right to run
- T2 10 to 20 ms: running
20 msThe two threads are interleaved, but never simultaneous: one core does both jobs one after the other, so the total is the sum.
Predict first
Two CPU-bound threads run in a GIL-enabled build. Compared with running the two jobs one after the other, the wall time is:
Two threads do CPU-only work: each needs 10 ms of pure computation, with no waiting on files or sockets. In a traditional CPython build only one thread at a time may execute Python bytecode, so the interpreter hands the right to run back and forth.
Common mistakes
- Treating the GIL as a general thread-safety guarantee: it serializes bytecode execution, not compound operations, so
counter += 1still interleaves between bytecodes. - Assuming free-threading makes every threaded program faster: removing the lock adds new synchronization costs, and workloads that are too small or too I/O-heavy will not repay them.
- Ignoring extension compatibility in a free-threaded deployment: compiled modules have to be built with awareness of the free-threaded ABI, so each native dependency is a question to check rather than assume.
- Generalizing a benchmark from one build to the other: the two builds differ by more than the lock (reference counting schemes, allocator behavior), so timings from one do not transfer silently.
Practice: Easy
- Run the probe and record which build you are on; then run the threading lesson's unlocked counter and note whether updates are still lost.
- Time a CPU-heavy loop single-threaded and split across two threads; record both numbers and explain them without attributing everything to one lock.
- Rewrite the probe as a one-line report function and call it from a script; note that no application logic should branch on the result.
Practice: Medium
- Compare a threaded CPU-heavy workload with the same work in separate processes and explain the gap from the execution model, not only from the lock.
- Research one native extension you use: find what its documentation says about free-threaded support and record the answer as a compatibility line item.
- Build the synchronization plan for a counter that must stay exact on both builds, and explain why one plan covers both.
Practice: Hard
- Design a checklist a team would follow before moving a service to a free-threaded build: interpreter selection, extension audit, benchmark plan and rollback condition.
- Build a failure lab: a GIL-dependent assumption that breaks under interleaving, a benchmark attributed to the wrong cause, and an extension that lacks free-threaded support; explain the mechanism behind each.
- Write two descriptions of the same program, one for readers who know the GIL and one for readers who do not, and check that neither claims a guarantee the language does not make.
Lesson checklist
- I can state what the GIL does and does not guarantee.
- I can explain why CPU-heavy threads stall while I/O-heavy threads still overlap.
- I can detect the running build with
sys._is_gil_enabled()and say what a missing attribute means. - I can name what free-threading changes and what stays the programmer's responsibility.
Remember
The GIL is one build's strategy for coordinating bytecode execution, and its disappearance in the free-threaded build changes performance behavior, not correctness obligations. Treat both builds as preemptive, keep protecting compound operations, and read every GIL claim as conditional on the interpreter it was measured on.
Interview angles
What is the main purpose of **What the Global Interpreter Lock is**?+
In a traditional CPython build, the GIL allows only one thread at a time to execute Python bytecode in a given interpreter. It simplifies parts of CPython's object model but limits CPU-bound Python-thread parallelism.
What should you pay attention to when using **What it does not mean**?+
In The GIL, What it does not mean names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
How does **CPU-bound threading limitations in CPython** fit into this lesson?+
In The GIL, CPU-bound threading limitations in CPython names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Why does removing the GIL not remove the need for locks?+
Removing the lock lets more threads execute bytecode at the same time, which widens the windows in which compound operations can interleave; synchronization is what makes an invariant hold within a thread-switching model, and the free-threaded build is still a preemptive model, just one whose threads actually run at once.