GIL (Global Interpreter Lock)
In one line
The GIL is a lock in CPython ensuring only one thread runs Python bytecode at a time, limiting threading parallelism.
In simple words
The Global Interpreter Lock (GIL) is a mutex in CPython (the standard Python implementation) that ensures only one thread executes Python bytecode at any given time.
Why it exists: CPython's memory management (reference counting) is not thread-safe. The GIL simplifies this by serializing bytecode execution.
Consequences:
- CPU-bound multi-threaded Python code does NOT run in parallel — threads take turns.
- I/O-bound code IS helped by threading — the GIL is released during I/O waits.
- Use multiprocessing or native extensions (NumPy, Cython) to bypass the GIL for CPU work.
Note: Python 3.13+ has an experimental free-threaded mode (PEP 703) that may remove the GIL.
Example
import threading
import time
def count(n):
while n > 0:
n -= 1
# Single thread
start = time.time()
count(50_000_000)
print(f"1 thread: {time.time() - start:.2f}s")
# Two threads — NOT faster due to GIL
start = time.time()
t1 = threading.Thread(target=count, args=(25_000_000,))
t2 = threading.Thread(target=count, args=(25_000_000,))
t1.start(); t2.start(); t1.join(); t2.join()
print(f"2 threads: {time.time() - start:.2f}s") # Same or slowerTwo threads doing CPU work are NOT faster than one — the GIL serializes bytecode execution. Use multiprocessing instead.
Common confusions
Confused with: thread lock
The difference: The GIL is an interpreter-level lock built into CPython that affects ALL threads globally. A thread lock (threading.Lock) is a user-level lock you create for specific shared data. The GIL is not something you control directly.
Related terms
Related Resources
Related Lessons
- Python Concurrency
Understanding the GIL.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.