Skip to content

Learn: Concurrency

Four notebooks, in order. Each is self-contained, but they build on each other — the first sets up the decision framework that the next three each fill in for one of Python's three tools.

Notebooks in this section

  1. Concurrency models — concurrency versus parallelism, the I/O-bound versus CPU-bound distinction that drives every choice, and a one-paragraph tour of the GIL. Ends with a map: which of Python's three tools fits which kind of work.
  2. Threads and futuresthreading.Thread, then the higher-level concurrent.futures.ThreadPoolExecutor you should actually reach for. submit, map, as_completed, collecting results and exceptions, and why shared mutable state needs a Lock.
  3. Processes and parallelismProcessPoolExecutor and multiprocessing for CPU-bound work that the GIL otherwise serialises. Pickling constraints, the if __name__ == "__main__" guard, and how to measure whether parallelism actually paid off.
  4. Async and await — coroutines, the event loop, asyncio.run, gather, and TaskGroup. How a single thread juggles thousands of waiting connections, plus cancellation, timeouts, and the cardinal rule: never block the loop.

After these, the Recipes cover task-focused applications and the Reference has quick lookups for asyncio, concurrent.futures, and the threading/multiprocessing primitives.