Threads, Processes and the GIL
What the interpreter lock actually locks, why an increment can lose a count, and when a process is the only thing that helps - with the mechanism runnable on every page.
About this track
Python's threads are real operating-system threads that are not allowed to run Python at the same time. One lock - the GIL - is held by whichever thread is executing bytecode, which is why threads help a program that waits and do nothing for a program that computes.
This track builds that from the bytecode up: the three operations an increment compiles to, the switch that lands between them, the lock protocol that prevents it, and the pickle boundary that decides what a process can be sent. A note on every page: the browser interpreter has no OS threads, so the editors run the mechanism and the thread-spawning examples are shown with their output.
All 7 modules, in teaching order
- 01The GIL, and What It Actually Locks
- 02Race Conditions: Why x += 1 Is Three Operations
- 03Locks, and the Four Ways They Go Wrong
- 04Threads or Processes: What Is and Is Not Shared
- 05concurrent.futures: One Interface for Both
- 06Handing Work Between Threads with a Queue
- 07Choosing Between Threads, Processes and Async