GIL
The Global Interpreter Lock (GIL) is a mutex in CPython that only one thread at a time may hold while executing Python bytecode. Threads overlap I/O; they do not run CPU-bound Python in parallel on multiple cores.
Detailed Explanation
CPython's objects have reference counts. Incrementing and decrementing those counts from two cores without a lock would corrupt memory. The GIL is the coarse lock CPython chose: a thread must hold it to run bytecode. The result is simple object semantics and a hard limit on CPU parallelism in pure Python.
I/O is the exception that makes threads useful. While a thread waits on a socket, a file, or a time.sleep, CPython releases the GIL. Another thread can run. A web scraper with fifty threads waiting on HTTP is faster than one thread, even with the GIL. A loop that multiplies numbers in Python is not.
C extensions can release the GIL around heavy C work. NumPy, lxml, and many compression libraries do this. Your Python loop that calls those functions can see multi-core speedups. Your Python loop that does the arithmetic itself cannot.
multiprocessing and ProcessPoolExecutor start extra interpreters, each with its own GIL and memory space. That is the standard way to use extra cores for CPU-bound Python. The cost is pickling data across processes.
Python 3.13 added an official free-threaded (no-GIL) build. It is optional, changes some performance trade-offs, and is not yet what most production installs run. Write code as if the GIL exists, and do not rely on it for thread safety of your own invariants. The GIL protects the interpreter, not your list.append sequences or your dict read-modify-write.
asyncio uses one thread and cooperative tasks. It is not a GIL workaround; it is an I/O multiplexer. Use it when the work is waiting, not crunching.
Code Examples
import sys
import threading
import time
def burn(n: int) -> int:
total = 0
for i in range(n):
total += i
return total
def timed(workers: int, n: int) -> float:
start = time.perf_counter()
threads = [threading.Thread(target=burn, args=(n,)) for _ in range(workers)]
for t in threads:
t.start()
for t in threads:
t.join()
return time.perf_counter() - start
# Two CPU-bound threads take ~2x one thread on CPython, not ~1x.
print(sys.implementation.name)# I/O-bound threads do overlap because the GIL is released around sleep.
def nap() -> None:
time.sleep(0.2)
start = time.perf_counter()
threads = [threading.Thread(target=nap) for _ in range(5)]
for t in threads:
t.start()
for t in threads:
t.join()
print(round(time.perf_counter() - start, 2)) # ~0.2, not 1.0Common Gotchas
Expecting eight threads to use eight cores on a Python loop
you get one core plus lock overhead. Use processes or a GIL-releasing library.
The GIL is not your lock
two threads can still interleave `balance -= 1` at the bytecode level. Use `threading.Lock` for invariants.
Free-threaded builds change the rules
code that "worked" because of the GIL may race. Write explicit locks either way.
`time.sleep` and I/O release the GIL
a demo that only sleeps is not a CPU benchmark.