Module 10 · Lesson 3

Async I/O

Run many I/O tasks concurrently without threads. Learn the event loop, promises, and futures.

Audio: Async I/O
0:000:00

Async I/O

Most programs spend more time waiting than computing. They wait for files, networks, databases, and users. Async I/O lets one thread juggle thousands of waits efficiently.

Blocking vs Non-blocking

In blocking I/O, your code calls read() and stops until data arrives. To handle many connections you would need many threads, each mostly idle.

In non-blocking I/O, the call returns immediately. Your program registers a callback, future, or promise to be notified when the operation finishes. While it waits, other work can run on the same thread.

The Event Loop

Async runtimes use an event loop: a queue of ready tasks that the runtime picks from one at a time. When a task hits an await, it pauses and the loop runs another ready task. When the awaited operation completes, the original task is added back to the queue.

Because tasks only switch at await points, there are no surprise context switches in the middle of a statement. That makes async code easier to reason about than threads.

Promises and Futures

A promise (or future) represents a value that will exist later. You attach handlers or await it to get the result.

Promise.all, asyncio.gather, std::future::get, and CompletableFuture.allOf start many tasks at once and wait for all of them. The total time is roughly the longest single task, not the sum.

When to Use What

  • Async I/O: many concurrent network or disk operations.
  • Threads: real CPU-bound work that needs multiple cores.
  • Both together: a thread pool of workers, each running an async loop.

Try It Yourself

  • Time the example sequentially (one await at a time) and in parallel. Confirm the parallel version finishes near the longest single delay.
  • Add a fourth task that throws an error and observe how your language reports it.
  • Write an async function that fetches results from a list of "urls" (use a sleep simulation) with a concurrency limit of two.

Code Playground

Edit the code below and click Run to see the output. Switch between languages using the tabs.

Loading editor...

Enjoying the lesson? Unlock the full Systems Programming & Concurrency from $4.99/mo.

See plans →