Python GIL: Global Interpreter Lock Explained
What you’ll learn
Section titled “What you’ll learn”- What the GIL is and why CPython has it (reference counting).
- Which workloads the GIL hurts (CPU-bound) and which it does not (I/O-bound).
- How
multiprocessingsidesteps the GIL with separate processes. - How C extensions like NumPy release the GIL during heavy work.
- The free-threaded (no-GIL) CPython builds from PEP 703, Python 3.13+.
Summary
Section titled “Summary”The GIL is a mutex in CPython that lets only one thread execute Python bytecode at a time, even on a multi-core machine. It exists because CPython’s memory management uses reference counting, which is not thread-safe by default; the GIL is the simplest way to make it safe without slower, fine-grained locking. The practical effect: threads do not speed up CPU-bound Python work, because only one runs at a time. But threads still help with I/O-bound work, because a thread releases the GIL while it waits on network, disk, or sleep. CPU parallelism comes from multiprocessing (separate processes, separate GILs) or from C extensions that release the GIL during their computation. Free-threaded builds that remove the GIL are arriving as an optional mode in Python 3.13+ (PEP 703).
The problem it solves
Section titled “The problem it solves”The GIL solves thread-safety for CPython’s memory model, not a performance problem. CPython tracks object lifetimes with reference counting: every variable, list element, and dict value holds a reference count, and decrementing it to zero triggers deallocation. Two threads mutating reference counts concurrently would corrupt them, leaking memory or freeing objects still in use. Fine-grained locking on every object would make Python slow. The GIL takes the blunt approach: only one thread runs Python bytecode at a time, so reference counting stays consistent without per-object locks.
This is a CPython implementation detail, not part of the Python language. Other interpreters (Jython, IronPython) do not have a GIL. But because CPython is the dominant interpreter, “Python” in practice means “GIL-affected Python.”
How it works
Section titled “How it works”CPU-bound threads do not parallelize
Section titled “CPU-bound threads do not parallelize”import threadingimport time
def cpu_bound(): total = 0 for i in range(50_000_000): total += i
start = time.perf_counter()t1 = threading.Thread(target=cpu_bound)t2 = threading.Thread(target=cpu_bound)t1.start(); t2.start()t1.join(); t2.join()print(f"two threads: {time.perf_counter() - start:.2f}s")Running this is roughly the same wall-clock time as running cpu_bound() twice sequentially, because only one thread holds the GIL at a time.
I/O-bound threads do benefit
Section titled “I/O-bound threads do benefit”import threadingimport time
def io_bound(): time.sleep(1)
start = time.perf_counter()threads = [threading.Thread(target=io_bound) for _ in range(5)]for t in threads: t.start()for t in threads: t.join()print(f"five sleeps: {time.perf_counter() - start:.2f}s") # ~1.0s, not ~5.0sWhile a thread sleeps or waits on I/O, it releases the GIL, so the others run concurrently. Five one-second sleeps finish in about one second.
multiprocessing sidesteps the GIL
Section titled “multiprocessing sidesteps the GIL”import multiprocessing
def cpu_bound(): total = 0 for i in range(50_000_000): total += i
if __name__ == "__main__": procs = [multiprocessing.Process(target=cpu_bound) for _ in range(2)] for p in procs: p.start() for p in procs: p.join()Each process has its own interpreter and GIL, so two CPU-bound processes run on two cores in parallel — about half the wall-clock time of one.
C extensions release the GIL
Section titled “C extensions release the GIL”Libraries like NumPy release the GIL around their heavy, compiled loops. A workload that spends most of its time inside NumPy array operations can benefit from threads, because that work is not Python bytecode and the GIL is released. You can do the same in a C extension or with Cython/ctypes by releasing the GIL around a long computation.
Free-threaded CPython (PEP 703)
Section titled “Free-threaded CPython (PEP 703)”PEP 703 introduces a free-threaded (no-GIL) CPython. As of Python 3.13 it is an experimental optional build; 3.14 improves it. In a free-threaded build, threads run Python bytecode in true parallel. The cost is changes to reference counting and new per-object locking, plus the ecosystem must rebuild C extensions for the free-threaded ABI. It is opt-in, not the default.
Result shape and edge cases:
- The GIL blocks concurrent Python bytecode execution, not waiting. I/O, sleep, and blocking C calls release it.
- A single-threaded CPU loop is unaffected — the GIL only limits concurrency, not single-threaded speed.
- Threads still have overhead (context switches, the GIL check interval); for light CPU work, threads can be slower than a single thread due to GIL contention.
- C extensions that do not release the GIL re-introduce the limitation even in threaded code.
Realistic example
Section titled “Realistic example”OrdersPortal has two workloads: fetching shipping rates from several carriers (I/O-bound, where threads help), and computing aggregate statistics over a large in-memory order dataset (CPU-bound, where threads do not help and multiprocessing does).
import threadingimport time
# (a) I/O-bound: call three carrier APIs concurrentlydef fetch_rate(carrier, order_id, results): time.sleep(0.5) # simulate a network call results[carrier] = f"{carrier}-quote-for-{order_id}"
def shipping_quotes(order_id): results = {} threads = [ threading.Thread(target=fetch_rate, args=("UPS", order_id, results)), threading.Thread(target=fetch_rate, args=("FedEx", order_id, results)), threading.Thread(target=fetch_rate, args=("DHL", order_id, results)), ] for t in threads: t.start() for t in threads: t.join() return results
start = time.perf_counter()print(shipping_quotes("ORD-1001")) # ~0.5s total, not ~1.5sprint(f"io-bound elapsed: {time.perf_counter() - start:.2f}s")The network calls overlap because each thread releases the GIL while waiting.
import multiprocessing
# (b) CPU-bound: sum over chunks of a large dataset -- threads would not helpdef total_amount(amounts): return sum(amounts)
def parallel_totals(chunks): with multiprocessing.Pool() as pool: return sum(pool.map(total_amount, chunks))
if __name__ == "__main__": chunks = [list(range(1_000_000)), list(range(1_000_000)), list(range(1_000_000))] print(parallel_totals(chunks))Here multiprocessing gives true parallelism; threading would run no faster than a single process.
Production patterns
Section titled “Production patterns”- Use threads for I/O-bound concurrency: HTTP calls, file reads, sleeps. The GIL does not get in the way.
- Use
multiprocessing(orconcurrent.futures.ProcessPoolExecutor) for CPU-bound parallelism; accept the higher memory cost and IPC overhead. - Offload CPU work to C extensions that release the GIL (NumPy, or a Cython/ctypes section), so threads can parallelize around it.
- For many independent I/O tasks, prefer
asyncioover threads — one thread, cooperative scheduling, no GIL contention. - Profile before parallelizing: the GIL is only the bottleneck for CPU-bound pure-Python work. I/O-bound code is rarely GIL-bound.
Common mistakes
Section titled “Common mistakes”- Assuming more threads speed up a CPU-bound Python loop — they do not; use
multiprocessing. - Using
multiprocessingwithout theif __name__ == "__main__":guard on Windows and macOS — causes recursive process spawning. - Expecting threads to share memory cheaply and then paying IPC overhead with
multiprocessing— there is no free lunch; choose by workload type. - Writing CPU-bound code and assuming the GIL is the only problem — the algorithm and data structures often matter more.
- Assuming free-threaded CPython is the default — it is opt-in and experimental as of 3.13.
When to use it and when not to
Section titled “When to use it and when not to”The GIL matters when:
- You have CPU-bound pure-Python work that you want to run on multiple cores. Use
multiprocessingor a GIL-releasing extension.
The GIL does not matter when:
- The work is I/O-bound (network, disk, sleep). Threads or
asynciogive concurrency without GIL limits. - The work is single-threaded. The GIL only limits concurrency.
- The heavy lifting is inside NumPy or another GIL-releasing extension. Threads can parallelize around that work.
Compatibility
Section titled “Compatibility”The GIL has been a CPython implementation detail since early multi-threading support. It is present in all CPython releases through 3.12. PEP 703 introduces free-threaded (no-GIL) builds; the feature is experimental and opt-in from Python 3.13, with stabilization in 3.14. To use it you build or install the free-threaded interpreter and C extensions must be built for the free-threaded ABI. Everything else here — threading, multiprocessing, and the I/O vs CPU distinction — applies unchanged on 3.9 through 3.14.
Interview questions
Section titled “Interview questions”-
Does adding more threads speed up a pure-Python CPU-bound loop? Why or why not? No. The GIL lets only one thread execute Python bytecode at a time, so CPU-bound threads do not run in parallel — two threads doing number crunching take roughly the same wall-clock time as one thread doing it twice.
-
Why does threading still help for I/O-bound tasks despite the GIL? A thread releases the GIL while it is blocked waiting on I/O (network, disk) or sleeping, letting other threads run during that wait. The GIL blocks concurrent Python bytecode execution, not waiting, so I/O-bound threads overlap their waits.
-
What is the standard-library way to get true CPU parallelism despite the GIL?
multiprocessing— separate OS processes, each with its own interpreter and GIL, genuinely run on separate cores.concurrent.futures.ProcessPoolExecutoris a convenient wrapper. -
Senior scenario: an OrdersPortal batch job calculates fraud scores over a million orders. The team tried
threading.Threadand saw no speedup. How would you parallelize it, and what tradeoffs would you flag? Threading did not help because the scoring is CPU-bound pure Python, so the GIL serialized it. The right move ismultiprocessing(orProcessPoolExecutor) to run scores on multiple cores in true parallel. Tradeoffs to flag: (a) each process has its own memory, so the order dataset must be partitioned and sent to workers (or loaded per worker), which raises memory use and IPC cost; for a million in-memory orders that can be significant; (b) on Windows and macOS,multiprocessingspawns processes, so the worker function must be picklable and guarded byif __name__ == "__main__"; (c) if the scoring is mostly inside NumPy or another GIL-releasing extension, threading could work instead, with shared memory and no IPC — check where the time actually goes before choosing. The fastest path is often to offload the hot computation to a GIL-releasing extension and then thread around it, or to partition the data across processes. Measure both.
Key takeaways
Section titled “Key takeaways”- The GIL lets only one thread run Python bytecode at a time; it protects CPython’s reference counting, not a performance feature.
- CPU-bound pure-Python threads do not parallelize; use
multiprocessingor a GIL-releasing extension. - I/O-bound threads overlap their waits because they release the GIL while blocked.
- NumPy and similar extensions release the GIL, so threads can parallelize around their work.
- Free-threaded (no-GIL) CPython is opt-in and experimental from 3.13 (PEP 703).
Related pages
Section titled “Related pages”- threading vs multiprocessing — choosing the right concurrency primitive
- async/await — single-threaded async concurrency for I/O-bound work
- generators — lazy evaluation, often used in async pipelines
- collections — data structures for grouping work before parallelizing