Grokking Concurrency in Python
Vote

0% completed

​

Course Introduction

Who This Course Is For

Harvest, the Program Behind Every Lesson

How the Lessons Work

The Chapters

Course Introduction
Course Introduction

A small Python program downloads eight pages from a slow website. One page at a time, the downloads take 2.06 seconds. With eight threads, they take 0.28 seconds. The same program then indexes the words on those pages, and eight threads finish no faster than one.

The cause of a result like this is rarely the code you are looking at. It is the model underneath it: what CPython does with threads, processes, and an event loop, and what each one costs. This course explains that model, measures it, and turns it into decisions you can defend.

Who This Course Is For

The course is for engineers who write Python at work and have used threads or async at least once. By the end, you will choose a concurrency model for a workload and support the choice with a measurement. You will write code that cancels, times out, fails, and shuts down cleanly, and you will test it and diagnose it in production. The course covers current CPython only.

Harvest, the Program Behind Every Lesson

Harvest is a small web crawler and search indexer: it downloads pages from a website and builds an index of the words on each page. The course builds it one piece at a time. Its work has two halves that behave very differently:

  • Fetching a page sends a request and waits for the reply, so the CPU has almost nothing to do. Threads and asyncio speed up this kind of work.
  • Indexing a page reads every word and counts it in plain Python, so a CPU stays busy from start to finish. Only parallel execution speeds up this kind of work.

Harvest must also respect a request rate limit, share a set of visited pages, recover from failures, and stop without losing work. The website is a test site that runs on the same machine. It answers each request after a fixed delay, 250 milliseconds unless a lesson says otherwise, so every timing can be repeated.

How the Lessons Work

Every program in the course ran, and its output is shown exactly as it was printed, except that file paths and similar machine details are replaced with placeholders. Every timing in the text comes from that output. All output comes from Python 3.15.2, on two machines, and each lesson names the machine that it used:

MachineUsed for
A desktop computer with an AMD Ryzen 7 5800X processor (8 cores, 16 logical CPUs) running WindowsWork running in parallel on several cores
A cloud virtual machine running Linux with 2 virtual CPUs (one physical core)Process start methods, signals, and production diagnostic tools

Many lessons run a program on both builds of CPython. The standard build, python3.15, has a global interpreter lock (GIL), a lock that lets only one thread run Python code at a time. The free-threaded build, python3.15t, runs without it by default. Python 3.14 is the baseline, Python 3.15 is the current release, and a lesson labels any feature that needs a newer version or a particular build.

Every lesson ends its teaching with an In Production section that describes the symptom in a running service and how to confirm the cause. Key Takeaways and four or five practice questions with folded answers follow. Chapters 1 to 14 each end with a quiz, and a final exam covers every chapter.

The Chapters

#ChapterWhat you will be able to do
1How CPython Runs Concurrent CodeTell waiting work from computing work, explain the GIL, and size a pool
2Threads Done RightStart and stop threads, find race conditions, and use locks, events, and queues
3Executors and FuturesUse thread and process pools, collect results and errors, and shut pools down
4ProcessesDecide when processes help, choose a start method, and control data copying
5asyncio From the Event Loop UpBuild a small event loop, then use coroutines and tasks
6Structured ConcurrencyGroup tasks so that one failure cancels the group and no error is lost
7Cancellation and TimeoutsCancel work, set deadlines, and write cleanup code that still runs
8Flow ControlLimit concurrency, rate limit, retry safely, and stream data in constant memory
9Mixing Sync and AsyncFind blocking calls in async code and connect the two styles safely
10Free-Threaded PythonMeasure the free-threaded build, find new data races, and plan a migration
11Subinterpreters and Choosing a ModelUse several interpreters in one process and choose a model per workload
12Testing Concurrent CodeTest async code, force races, test timeouts without waiting, and stress test
13Diagnosing Problems in ProductionFind a blocked loop, a stuck task, a deadlock, a leak, and GIL contention
14Production PatternsShut down gracefully, supervise workers, trace requests, and size a service
15Capstone: Harvest End to EndReview the finished design and test how it behaves under failure
16Practice ProblemsBuild a blocking queue, a read-write lock, a shared rate limiter, and a crawler

Read the chapters in order, because each one builds on the model of the chapters before it.

The course begins with the measurement at the top of this page: eight threads made Harvest's downloads seven times faster and left its indexing unchanged. The first lesson, Concurrency and Parallelism in Python, explains why, and defines the two terms that the rest of the course depends on.

Reading Progress

0%


Vote for new content

On This Page

Who This Course Is For

Harvest, the Program Behind Every Lesson

How the Lessons Work

The Chapters