Programming Paradigms · Session 6 · Asynchronous programming

Why study this

One thread, a queue, and a loop that runs one function at a time: the plan behind most of the software we touch every day. Session 6 is about how to program on it: the properties it guarantees, and the patterns that recur from one language to the next.

the host network · clock · clicks appends the queue the looptakes one one thread runs it to its end at an await, the rest of the function waits for its value, then rejoins the queue
The model of the session. Everything below runs on it.

Five places it runs

Every web pageProperty 3

A page's JavaScript runs on one thread; the HTML standard defines the event loop that feeds it clicks, timers and network replies. A long computation on it freezes the page.

Node.jsProperty 2

In 2009 Ryan Dahl put that loop on the server: one thread serving many connections, waiting on all of them at once. Today it runs servers, build tools and command-line programs; its loop is the C library libuv.

nginx and RedisProperty 4

nginx serves a large part of the web with one event loop per worker. Redis executes every command on one thread, and that is why each command is atomic with no lock at all.

Every app with a windowProperty 3

Android's Looper, Apple's RunLoop, the main thread of VS Code (Electron): a user interface is one loop handling one event at a time. “The app is not responding” is a function that did not return.

AI agentsProperties 2 and 4

A call to a language model takes seconds. An agent mostly waits: it awaits the model, runs the tools it asked for together (asyncio.gather), and awaits the model again. The model providers' Python libraries have asynchronous clients, and so do the Model Context Protocol's SDKs.

Learning objectives

In the session

  1. Predict what a short program with async functions prints, and justify it: where each function is suspended, and what is waiting in the queue.
  2. Find and repair a lost update: mark, in the text of a function, the stretches during which no other code of ours runs; find a read, a wait and a write that lose an update; repair it.
  3. Find out, for a language with await we have not seen, by three small runs: whether a call starts the work, whether every await suspends, which thread resumes.

In the lab's optional quest

  1. Write the rest of a computation as a function, its continuation, and rewrite a small function to take one.
  2. Trace one wait from end to end through a promise, a queue and a loop, and say what each of them holds.

Draft, 7 October 2026. To check before publishing: the sentence on nginx's share of the web (W3Techs), and that the async clients and the MCP SDK are as described in the current versions of the repositories linked. Logos are each organization's GitHub avatar.