Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 12.2 — The GIL and Free-Threaded Python
Python · Example 2 of 2

counter += 1 is three steps

Read the instructions between RESUME and RETURN. Exact bytecode differs between Python versions, but the read, add, store shape does not.

The Global Interpreter Lock (GIL) lets only one thread run Python bytecode at a time in the standard CPython build, so threads do not speed up pure-Python calculation but still help with waiting. A free-threaded build without the GIL exists (experimental in 3.13, officially supported in 3.14).

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Print whether your interpreter runs with the GIL, in the form GIL: True, using a safe fallback for versions older than 3.13.

02

Use dis to show the bytecode of a function bump(d, k) that does d[k] += 1. Which instructions read, add and store?

03

Explain in one sentence each why list.append from many threads does not corrupt the list, while counter += 1 can lose updates.

Explain it without notes

01

What is the GIL, and what does it protect?

02

Why do threads speed up an I/O-bound program but not a CPU-bound pure-Python one?

03

If the GIL exists, why do you still need locks in threaded Python code?

04

What are the free-threaded build and sub-interpreters, and what is their status?

05

Your threaded counter test passes on your machine, even with 4 threads and a million increments each and no lock. Is the code safe? What would you do?

counter += 1 is three steps
Sign in to run this example in your browser.

Expected output

  6           RESUME                   0

  8           LOAD_GLOBAL              0 (counter)
              LOAD_CONST               1 (1)
              BINARY_OP               13 (+=)
              STORE_GLOBAL             0 (counter)
              RETURN_CONST             0 (None)