Stop 14 · Advanced PythonTopics · 3/8

The Global Interpreter Lock (GIL)

Part of the Python Roadmap

In short

"Python can't do real multithreading" is one of the most repeated lines in tech. The truth is more precise, and explaining it well is a favourite interview question.

The GIL in CPython lets only one thread run Python bytecode at a time, so threads don't speed up CPU-heavy Python code, though they still help with I/O. Python 3.13 added an experimental free-threaded build that removes it.

The jaadu holds the only microphone on a college debate stage while three other speakers wait in a row of chairs, flipping through their notes.

Think of it like this

The single microphone

A college debate has four speakers but only one microphone. Speakers can think, read notes or wait for their turn at the same time, but only the one holding the mic can speak. CPython's GIL is that microphone: many threads can wait on networks or files together, but only one can run Python bytecode at any moment.

Watch it happen

Two threads sharing the GIL

Thread A
GIL
Thread B

acquire

1/5Thread A takes the GIL and runs Python bytecode. Thread B must wait.

The big ideas

What you need to know

What it is

A lock inside CPython that lets only one thread execute Python bytecode at a time. It keeps reference counts and internals safe.

I/O still overlaps

Waiting on a socket, file or time.sleep releases the GIL, so threaded downloads or API calls still get much faster.

C extensions can release it

NumPy, hashlib and many compression and image libraries release the GIL during heavy work, so they can use several cores.

Free-threaded Python

Python 3.13 added an optional free-threaded build without the GIL (PEP 703). Standard Python 3.12 installs still have it.

See it in code

Walk through the code

python
1import threading2import time3 4def countdown(n: int) -> None:5    while n > 0:6        n -= 17 8N = 20_000_0009 10start = time.perf_counter()11countdown(N)12print(f"one thread:  {time.perf_counter() - start:.2f}s")13 14start = time.perf_counter()15t1 = threading.Thread(target=countdown, args=(N // 2,))16t2 = threading.Thread(target=countdown, args=(N // 2,))17t1.start()18t2.start()19t1.join()20t2.join()21print(f"two threads: {time.perf_counter() - start:.2f}s")

1/4A pure Python loop with no I/O. It needs the GIL the whole time.

The classic GIL demo. Splitting a CPU-bound loop across two threads gives little or no speedup on standard CPython.

Save yourself the pain

Common mistakes

Oops

Saying "Python threads are useless".

Do this instead

They are very useful for I/O-bound work. The GIL only limits CPU-bound pure Python code.

Oops

Assuming the GIL makes your own code thread-safe.

Do this instead

The GIL protects interpreter internals, not your logic. count += 1 from many threads can still lose updates, so use a Lock.

Oops

Thinking the GIL is part of the Python language.

Do this instead

It is a detail of CPython. Implementations like Jython never had one, and CPython now offers a free-threaded build.

Your mission

Prove it on your laptop

~20 min

Quick check

Which statement about the GIL in standard CPython 3.12 is correct?

Go deeper

  1. 1Time a CPU-heavy loop with one thread and with four threads and compare.
  2. 2Repeat the test with an I/O-bound task like downloading pages.
  3. 3Read the Python release notes about the free-threaded build.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge