On this page
What is the Python GIL?
What the GIL stops: parallel Python code
What the GIL does not stop
Threads still overlap while they wait or run C code
The GIL never made your code thread-safe
What free-threaded Python changes
How much faster are threads without the GIL?
What removing the GIL costs
Should you use free-threaded Python?
Frequently asked questions
Related reading
The Python GIL Explained, and What Free-Threaded Python Changes


On This Page
What is the Python GIL?
What the GIL stops: parallel Python code
What the GIL does not stop
Threads still overlap while they wait or run C code
The GIL never made your code thread-safe
What free-threaded Python changes
How much faster are threads without the GIL?
What removing the GIL costs
Should you use free-threaded Python?
Frequently asked questions
Related reading
The Python GIL (global interpreter lock) is a single lock inside CPython that a thread must hold to run Python bytecode, so only one thread in a process runs Python code at any moment. Threads still help a program that waits on the network or the disk, because a waiting thread releases the lock. They do not make pure Python computation faster, and the GIL never made your own code thread-safe.
Free-threaded Python is a separate build of CPython without the GIL. On that build, threads run Python code on several CPUs at the same time, which makes CPU-heavy threads much faster. It also makes a program with one thread somewhat slower in some jobs, and it exposes data races that the GIL used to hide.
This article explains what the GIL is, what it stops, what it does not stop, and what the free-threaded build gains and costs. Every number comes from one run of Python 3.15.2, on either an 8-core desktop (AMD Ryzen 7 5800X, 16 logical CPUs) or a Linux cloud runner with 2 virtual CPUs on one physical core. Each table names its machine.
What is the Python GIL?
CPython, the standard Python interpreter, keeps a lot of internal state that every thread uses. Every object stores a count of how many references point to it, and CPython frees the object when that count reaches zero (a reference count). Built-in types such as dict and list also keep internal tables and arrays, and CPython has its own memory allocator.
If two threads changed one reference count at the same instant, one change could be lost. CPython could then free an object that is still in use, and the process would crash or corrupt memory. The GIL prevents this with one simple rule: a thread must hold the lock to run Python code, so only one thread at a time touches that state.
The GIL protects the interpreter's own state, not the meaning of your data. It guarantees that a reference count or a dictionary's table is never left half changed. It does not guarantee that a series of steps in your code runs without another thread running in between.

A thread does not hold the GIL forever. It releases the lock when it waits for input or output, when C code that does not need the interpreter runs, and when another thread has waited longer than the switch interval. The switch interval is the time a waiting thread allows before it asks the running thread to give up the lock, and its default is 5 milliseconds.
What the GIL stops: parallel Python code
The clearest effect of the GIL shows up when threads do pure Python computation. The test below indexes pages of text: it splits each page into words and counts them in a dictionary, which is plain Python code. It ran on the Ryzen desktop with Python 3.15.2.
| Job, Python 3.15.2 on the Ryzen desktop | Standard build (GIL on) | Free-threaded build (GIL off) |
|---|---|---|
| Index pages, one at a time | 1.51 s | 1.61 s |
| Index pages, 8 threads | 1.54 s | 0.38 s |
| Index pages, 8 processes | 0.28 s | 0.36 s |
With the GIL on, 8 indexing threads took 1.54 seconds, slightly longer than one thread doing all the work. The threads took turns, so the program never ran faster than one CPU allowed. A second program, which measured CPU time while its threads indexed pages, confirms it: the threads used 1.64 seconds of CPU time in 1.65 seconds of wall time, which means 1.0 CPUs were busy. On the free-threaded build, the same threads kept 7.0 CPUs busy.
Eight processes avoided the problem on the standard build, finishing in 0.28 seconds. Each process has its own interpreter and its own GIL. That is why multiprocessing and ProcessPoolExecutor have been the standard answer for CPU-heavy Python work.
You can check which build you run, and how many CPUs your threads keep busy, with the standard library alone. This script uses the same indexing function as the test above:
import platform import random import sys import sysconfig import time from concurrent.futures import ThreadPoolExecutor WORDS = ("python thread process event loop queue lock worker crawler index search page link " "request reply socket memory cache server client parallel concurrent timeout").split() def index(text): counts = {} for word in text.split(): key = 0 for ch in word: key = (key * 31 + ord(ch)) % 1_000_003 counts[key] = counts.get(key, 0) + 1 return counts def make_pages(count, words): return [" ".join(random.Random(n).choice(WORDS) for _ in range(words)) for n in range(count)] if __name__ == "__main__": build = "free-threaded" if sysconfig.get_config_var("Py_GIL_DISABLED") else "standard" print(f"Python {platform.python_version()}, {build} build, GIL enabled: {sys._is_gil_enabled()}") pages = make_pages(8, 200_000) with ThreadPoolExecutor(max_workers=8) as pool: wall, cpu = time.perf_counter(), time.process_time() list(pool.map(index, pages)) wall, cpu = time.perf_counter() - wall, time.process_time() - cpu print(f"8 indexing threads: wall time {wall:.2f} s, CPU time {cpu:.2f} s, {cpu / wall:.1f} CPUs busy")
On the Ryzen desktop, the two builds printed this:
$ python3.15 gil_state.py Python 3.15.2, standard build, GIL enabled: True 8 indexing threads: wall time 0.62 s, CPU time 0.62 s, 1.0 CPUs busy $ python3.15t gil_state.py Python 3.15.2, free-threaded build, GIL enabled: False 8 indexing threads: wall time 0.14 s, CPU time 0.80 s, 5.7 CPUs busy
CPU time divided by wall time is the number of CPUs your threads kept busy. A value near 1.0 with several busy threads means they are taking turns for the GIL.
What the GIL does not stop
The GIL limits Python code, not every kind of work a thread does. It also limits less than many engineers believe about safety.
Threads still overlap while they wait or run C code
A thread that waits on a socket releases the GIL, so other threads run during the wait. That is why 8 threads fetched pages in 0.28 seconds against 2.06 seconds one at a time, even with the GIL on. Many C functions in the standard library and in libraries such as NumPy also release the GIL while they work on data that the interpreter does not need to see.
| Step for 8 pages, Python 3.15.2 standard build, Ryzen desktop | 1 thread | 8 threads | Speedup |
|---|---|---|---|
| Index (Python code) | 2,455 ms | 2,437 ms | 1.0x |
Compress (zlib, level 9) | 1,463 ms | 186 ms | 7.9x |
Fingerprint (hashlib.sha256) | 12 ms | 2 ms | 7.2x |
| Sort 2,000,000 scores (NumPy) | 273 ms | 62 ms | 4.4x |
With the GIL on, threads made zlib compression 7.9 times faster and gave Python code no gain. The useful question is therefore not "does Python have a GIL?" but "does my thread spend its time in Python code, or in C code that releases the lock?"
The GIL also delays threads that need the CPU only briefly. On the Linux runner, a health-check thread that asked to wake every 1 millisecond woke a median of 0.1 milliseconds late when the process was idle. While another thread indexed two pages, the median delay grew to 5.1 milliseconds, the length of the switch interval, because the thread woke on time and then waited for the GIL. On the free-threaded build, it stayed at 0.1 milliseconds.
The GIL never made your code thread-safe
A line such as total += 1 looks like one step, but CPython compiles it into several bytecode instructions: read the value, add one, and write the result back. Another thread can run between those instructions. When two threads read the same old value, both compute a new value from it, and the second write replaces the first (a lost update).
The test below started 4 threads that each updated shared values 1,000,000 times. It ran on the Linux runner with Python 3.15.2:
def add_words(): global words_seen for _ in range(1_000_000): words_seen += count_words(LINE) # LINE has 8 words
| Shared update, 4 threads, Linux runner | GIL on: lost | GIL off: lost |
|---|---|---|
pages_indexed += 1 (4,000,000 expected) | 0 | 2,400,953 |
words_seen += count_words(LINE) (32,000,000 expected) | 19,185,792 | 21,733,640 |
| The same update, holding a lock | 0 | 0 |
fingerprints.append(n) (4,000,000 expected) | 0 | 0 |
With the GIL on, the update that calls a function lost 60 percent of its updates. The function call is one of the points where CPython checks whether to switch threads, so a thread can be paused while it holds a stale value. The plain pages_indexed += 1 lost nothing on the standard build, but that is a detail of where the current interpreter happens to switch, not a promise. On the free-threaded build, the same line lost 2,400,953 of 4,000,000 updates.
Two lines stayed correct on both builds. The lock made each read, add, and write one unit, and list.append is a single operation on the list's own state. The rule is short: one operation on a built-in object is safe, and a sequence of operations is your responsibility.
The course behind these measurements runs each experiment on both builds and explains why each result came out as it did.
Want to see every one of these runs in full? Grokking Concurrency in Python covers threads, processes, asyncio and the free-threaded build, with measured output from real programs on stated machines.
What free-threaded Python changes
Removing the GIL meant giving each part of the interpreter's state its own protection. The design is described in PEP 703, the Python Enhancement Proposal that introduced free threading, and it was accepted on the condition that the change stay gradual and reversible. That condition is why the standard build still has the GIL by default.
| The GIL used to protect | The free-threaded build uses |
|---|---|
| Reference counts | Biased reference counting, immortal objects, deferred counting |
Internal tables of dict, list, set | A small lock inside each object |
| The memory allocator | mimalloc, an allocator that is safe for many threads |
| Garbage collection | A short pause of all threads while the collector runs |
The free-threaded build is a separate installation, usually started as python3.15t, and python3.15t -VV prints Python 3.15.2 free-threading build. The python.org installers offer it as an option, and tools such as uv can install it next to the standard build. Two checks tell you what a process is really doing: sysconfig.get_config_var("Py_GIL_DISABLED") says which build it is, and sys._is_gil_enabled() says whether the GIL is active right now.
The GIL can come back on the free-threaded build. The environment variable PYTHON_GIL=1, or the option -X gil=1, turns it on for the whole process. On the Ryzen desktop, PYTHON_GIL=1 python3.15t gil_state.py reported 0.66 seconds and 1.0 CPUs busy, the same as the standard build. The reverse does not work: PYTHON_GIL=0 on the standard build stops with a fatal error, because that build cannot run without the GIL.
An import can also turn the GIL on without warning you clearly. When the free-threaded build loads a C extension that has not declared it can run without the GIL, it enables the GIL for the whole process and prints a single RuntimeWarning. In the course's test on the Ryzen desktop, importing such a module, which the program never called, took 8 indexing threads from 6.7 busy CPUs to 1.0.
How much faster are threads without the GIL?
The next test indexed 16 pages of 60,000 words with more and more threads. It ran on the Ryzen desktop, and each value is the median of 3 runs:
| Threads | Standard build | Free-threaded build |
|---|---|---|
| 1 | 0.36 s (1.0x) | 0.40 s (1.0x) |
| 2 | 0.35 s (1.0x) | 0.20 s (2.0x) |
| 4 | 0.37 s (1.0x) | 0.12 s (3.5x) |
| 8 | 0.35 s (1.0x) | 0.08 s (5.3x) |
| 16 | 0.36 s (1.0x) | 0.06 s (6.7x) |
The standard build stayed at 1.0x for every thread count. The free-threaded build scaled to 6.7x at 16 threads. It did not reach 16x because the machine has 8 physical cores, and the second logical CPU on each core does not double that core's speed.
What removing the GIL costs
The gain has three costs, and each one depends on what your code does.
One thread can be slower. The free-threaded build does extra work to keep reference counts and containers safe, even when only one thread runs. On the Ryzen desktop, with the median of 3 runs, the same single-threaded jobs compared like this:
| Job, one thread | Standard | Free-threaded | Difference |
|---|---|---|---|
| Index 16 pages | 0.36 s | 0.40 s | +13% |
| Build 1M objects | 0.48 s | 0.35 s | -28% |
| Sort 1M tuples | 0.81 s | 0.87 s | +8% |
The Python documentation reports an average cost of about 1 percent on macOS with ARM processors and 8 percent on x86-64 Linux across the pyperformance benchmark suite. These three jobs show why an average is not enough: one was 13 percent slower and one was faster. Measure your own workload before you decide.
A shared lock can make parallel threads slower than one thread. Eight threads that kept one shared dictionary, protected by one lock taken for every word, took 3.08 seconds on the free-threaded build. With a private dictionary per thread, merged at the end, the same work took 0.08 seconds. The threads spent most of their time waiting for that one lock (lock contention). On the standard build, the same lock cost little, because the threads already ran one at a time.
Races that the GIL hid now appear often. The test below ran 8 threads on shared objects, 200,000 times each, on the Ryzen desktop:
| Shared operation, 8 threads, Ryzen desktop | GIL on | GIL off |
|---|---|---|
stats.pages += 1 (1,600,000 expected) | 1,600,000 | 400,750 |
counts[k] = counts.get(k, 0) + 1 (1,600,000 expected) | 1,600,000 | 276,791 |
found.append(url) (1,600,000 expected) | 1,600,000 | 1,600,000 |
next() on one shared generator | 24,297 errors | 70,646 errors |
The dictionary update kept only 276,791 of 1,600,000 counts, so most updates were lost, yet the dictionary itself stayed consistent. The append stayed exact. A generator shared between threads raised ValueError: generator already executing on both builds, three times as often without the GIL. Code that was unsafe with the GIL stays unsafe without it, and its errors become far more frequent.
Should you use free-threaded Python?
The decision depends on where your program spends its time and on what its threads share. The table compares the three ways to run CPU-heavy Python work in parallel:
| Standard build, threads | Standard build, processes | Free-threaded build, threads | |
|---|---|---|---|
| Python code runs in parallel | No | Yes | Yes |
| Data sharing | Shared memory | Copied between processes | Shared memory |
| Single-thread speed | Baseline | Baseline | Varies by job, measure it |
| Main risk | No CPU speedup | Startup and copying cost | Data races, C extensions that turn the GIL on |
A short checklist covers most cases:
- Your threads mostly wait on the network or disk. Keep the standard build. The GIL is released while threads wait, so free threading adds little.
- Your hot loops run in C code that releases the GIL, such as
zlib,hashlibor NumPy. Threads on the standard build already run in parallel there. - Your threads run pure Python computation and share little data. The free-threaded build can give a large speedup, 6.7x at 16 threads in the test above. Check that every C extension you import supports it.
- Your threads update shared state. Audit every read, modify, and write sequence and protect it with a lock, or give each thread its own data and merge the results afterward.
- Something breaks or runs slowly after a switch. Set
PYTHON_GIL=1to turn the GIL back on without changing the installation, then compare the two runs.
The GIL is best understood as two separate facts. It limits Python code to one CPU, which the free-threaded build removes. It never protected the logic of your program, and nothing about that changes.
Frequently asked questions
Is Python multithreading truly parallel? On the standard build, Python threads run Python code one at a time, so pure computation does not speed up. They do overlap while they wait for input or output and while C code that releases the GIL runs. On the free-threaded build, threads run Python code on several CPUs at the same time.
Is the GIL removed from Python?
Not from the standard build. The free-threaded build is a separate installation, usually started as python3.15t, that runs without the GIL by default. The standard build keeps the GIL, because PEP 703 was accepted on the condition that the change stay gradual and reversible.
How do I check whether the GIL is enabled?
Call sys._is_gil_enabled(), which returns True when the GIL is active in the current process. To check the build itself, use sysconfig.get_config_var("Py_GIL_DISABLED"), which returns 1 on a free-threaded build. The two can differ, because the GIL can be turned on at startup or by an import.
What does the PYTHON_GIL environment variable do?
On the free-threaded build, PYTHON_GIL=1 turns the GIL on for the whole process, and PYTHON_GIL=0 keeps it off even after importing an extension that has not declared support. The option -X gil=1 does the same as PYTHON_GIL=1. On the standard build, PYTHON_GIL=0 stops Python with a fatal error.
Is free-threaded Python slower for single-threaded code? Sometimes. In the tests above on Python 3.15.2, one thread was 13 percent slower for indexing, 8 percent slower for sorting, and 28 percent faster for building objects. The cost depends on the code, so measure your own workload.
Related reading
- Concurrency vs Parallelism: Why They're Not the Same Thing
- How many threads can be executed at a time?
- What is Race conditions?
- Deadlock vs Livelock: Key Differences and How to Prevent Both
Ready to write concurrent Python that is fast and correct? Grokking Concurrency in Python teaches threads, processes, asyncio and the free-threaded build with measured runs, and ends with a full concurrent crawler you build and test.
What our users say
Tonya Sims
DesignGurus.io "Grokking the Coding Interview". One of the best resources I’ve found for learning the major patterns behind solving coding problems.
Steven Zhang
Just wanted to say thanks for your Grokking the system design interview resource (https://lnkd.in/g4Wii9r7) - it helped me immensely when I was interviewing from Tableau (very little system design exp) and helped me land 18 FAANG+ jobs!
Roger Cruz
The world gets better inch by inch when you help someone else. If you haven't tried Grokking The Coding Interview, check it out, it's a great resource!
Access to 50+ courses
New content added monthly
Certificate of completion
$33.17
/month
Billed Annually
Recommended Course

Grokking Dynamic Programming Patterns for Coding Interviews
13,421+ students
4.4
Grokking Dynamic Programming Patterns for Coding Interviews in Python, Java, JavaScript, and C++. A complete guide to grokking dynamic programming.
View Course