Python, properly · Chapter 39 of 42

Concurrency

By the end of this chapter you can tell work that waits from work that computes, speed up the first with threads or async and the second with processes, call blocking code from async code, and protect shared data with a lock. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Real-world Python
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

Month end. Rafiq's report script asks the old order system about every order (each answer takes a while) and then crunches numbers over a big export. It takes twenty minutes, and he runs it four times a month.

Last chapter, async made waiting overlap. But Rafiq's script uses a library with no async version, and half its time is spent computing, not waiting, where async does nothing at all. Python has three tools for doing several things at once: threads, processes and async. Each one wins at a different kind of work, and picking the wrong one makes code slower and buggier. This is also a favourite interview topic: "what is the GIL?"

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    threads_io.py: slow calls in threads

    Rafiq's report script uses plain, blocking httpx calls. GET http://localhost:8000/slow?ms=500 answers after 500 ms with {"waited_ms": 500}.

    fetch_threaded(ms_list, workers=8) calls /slow once for each number in ms_list, in a pool of at most workers threads, and returns the waited_ms of each answer in the order of ms_list: fetch_threaded([300, 100, 200]) returns [300, 100, 200].

    The check times four calls of 800 ms (well under 3.2 seconds) and four of 300 ms with workers=2 (two rounds, so about 0.6 seconds).

  2. Task 2

    cpu_split.py: a process for each job

    busy(n) in the file is pure computing, which threads cannot speed up. split_work(jobs) runs busy(n) for each n in jobs, each in a Process of its own from multiprocessing, all started before any is waited for. Each process sends its result back through a Pipe; collect them and join every process.

    It returns the results in the order of jobs. The check records which process each busy call ran in: none may run in your own program, and each job must run in a different process.

    ProcessPoolExecutor needs /dev/shm, which this lab does not have: use Process and Pipe.

  3. Task 3

    to_thread.py: blocking code in an async program

    legacy_lookup(order_id) in the file comes from the old order system's library: it takes 0.4 seconds, it blocks, and you cannot change it.

    Write the coroutine lookup_all(order_ids) that runs legacy_lookup for every id at the same time without freezing the event loop, and returns its answers in the order of order_ids: lookup_all(["D0001", "D0004"]) returns ['delivered', 'out_for_delivery'].

    The check runs a small ticking task beside lookup_all: it must keep ticking while the lookups run, and four lookups must take well under 1.6 seconds.

  4. Task 4 · interview question

    Interview: race.py, the lost tips

    Riders get tips from many threads at once, and Rafiq finds the totals too low. TipJar.add in the file reads a rider's total, waits for the slow payments system, then writes the new total, and tips go missing.

    Make add safe with a threading.Lock so every tip is counted. Keep the slow step where it is, between the read and the write. total(rider_id) returns 0 for a rider with no tips.

    The check runs 8 threads at once (6 for R1, 2 for R2), each adding 20 tips of 10: R1 must end at 1200 and R2 at 400.

What you will learn

  • I/O-bound and CPU-bound: the two kinds of slow, and how to tell them apart.
  • Threads with ThreadPoolExecutor, and the GIL, which keeps threads from computing in parallel.
  • Processes with multiprocessing: their own interpreter, their own memory; asyncio.to_thread for blocking code in async programs.
  • Race conditions and threading.Lock.

More in Real-world Python

Build "Concurrency" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.