Python · Concurrency & Runtime

What is the GIL?

Hardgilcpythonthreadingconcurrency

Interview question

What is the GIL (Global Interpreter Lock) in Python?

How to answer

The short answer

In CPython, the Global Interpreter Lock (GIL) is a mutex that allows only one thread to execute Python bytecode at a time in a single process.

Why it exists

  • Simplifies memory management (especially reference counting).
  • Makes C extension integration safer historically.

Practical impact

  • CPU-bound multi-threading does not get true parallel speedup on multiple cores for pure Python bytecode.
  • I/O-bound threads can still help: the GIL is released around many blocking I/O operations, so threads can overlap waits.
  • For CPU parallelism, prefer multiprocessing, process pools, native extensions that release the GIL, or alternative runtimes.
Python
import threading

counter = 0

def bump(n):
    global counter
    for _ in range(n):
        counter += 1   # not atomic; still needs locks for correctness

threads = [threading.Thread(target=bump, args=(100_000,)) for _ in range(4)]
for t in threads:
    t.start()
for t in threads:
    t.join()
print(counter)  # often not 400_000 without a Lock

Interview nuance: The GIL is a CPython implementation detail, not part of the Python language spec. Also distinguish "GIL prevents parallel bytecode" from "threads are useless" (they are useful for I/O).

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill