Learn threading & multiprocessing

Run Python work in parallel with threads, processes and concurrent.futures

14 lessons · 3 hours · 4 levels · covers threading & multiprocessing Python 3.13

Python's standard library offers threads (threading), processes (multiprocessing), thread-safe queues and the high-level concurrent.futures executors for running work concurrently. Threads speed up I/O-bound programs, processes sidestep the GIL for CPU-bound work, and Python 3.13 adds an experimental free-threaded build. Learning these tools lets you use every core and overlap slow I/O safely.

Before you start

threading & multiprocessing builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 38 min in total
2. PracticalUse it productively at work, with the usual idioms8 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs11 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know14 lessons · 3 hours in total

The threading & multiprocessing tree

threading & multiprocessingStarting threadsStarting threadsThe GIL: I/O-bound vs CPU-boundThe GIL: I/O-boundvs CPU-boundThreadPoolExecutorThreadPoolExecutorRace conditions and LockRace conditions andLockProducer-consumer with queue.QueueProducer-consumerwith queue.QueueFutures: submit, as_completed and errorsFutures: submit,as_completed and…ProcessPoolExecutor for CPU-bound workProcessPoolExecutorfor CPU-bound workEvents, conditions and semaphoresEvents, conditionsand semaphoresDefining functionsDefining functionsContext managers and withContext managersand withExceptions in depthExceptions in depth
Essentials and Practical shown (8 of 14 lessons); the full list is below. Click a lesson to read it.

All 14 lessons, in order

Threads

  1. Starting threads · Essentials · 9 min

    Run functions concurrently with threading.Thread, start() and join().

  2. The GIL: I/O-bound vs CPU-bound · Essentials · 10 min

    Understand why threads speed up I/O-bound work but not CPU-bound pure-Python work on a standard CPython build.

Executors and futures

  1. ThreadPoolExecutor · Essentials · 9 min

    Run many I/O tasks on a reusable pool of threads with concurrent.futures.ThreadPoolExecutor and map().

  2. Futures: submit, as_completed and errors · Practical · 11 min

    Use executor.submit to get Future objects, process results as they finish, and handle exceptions and cancellation.

  3. ProcessPoolExecutor for CPU-bound work · Practical · 11 min

    Use all CPU cores with ProcessPoolExecutor, and respect its rules: the __main__ guard and picklable arguments.

Synchronisation

  1. Race conditions and Lock · Essentials · 10 min

    See how unsynchronised updates to shared data lose writes, and protect critical sections with threading.Lock.

  2. Producer-consumer with queue.Queue · Practical · 11 min

    Pass work between threads safely with queue.Queue, task_done/join, and the new Queue.shutdown in 3.13.

  3. Events, conditions and semaphores · Practical · 11 min

    Coordinate threads with Event, Semaphore, Condition and Barrier instead of busy-waiting.

  4. Thread-safety patterns and deadlocks · Advanced · 12 min

    Avoid deadlocks with lock ordering and timeouts, use thread-local storage, and shut down daemon and worker threads cleanly.

Processes

  1. multiprocessing and start methods · Advanced · 13 min

    Use multiprocessing.Process and Pool directly, and choose between the spawn, fork and forkserver start methods.

  2. Sharing data between processes · Advanced · 13 min

    Exchange data between processes with Queue and Pipe, share memory with Value, Array and shared_memory, or use a Manager.

GIL and runtime internals

  1. How the GIL works · Expert · 13 min

    Understand GIL hand-offs, the switch interval, why C extensions can release it, and what the GIL does and doesn't protect.

  2. Free-threaded Python (no GIL) · Expert · 13 min

    Run threads truly in parallel with the experimental free-threaded CPython 3.13 build, and know its costs and compatibility rules.

  3. Fork safety, initializers and process lifecycles · Expert · 12 min

    Avoid fork-with-threads deadlocks, reset state with os.register_at_fork and pool initializers, and manage worker lifecycles.