liveBook · Manning

Asynchronous programming

As explained in Rust Servers, Services, and Apps

Asynchronous programming is a technique in which tasks can suspend during external operations and allow other tasks to make progress before resuming.

Asynchronous programming lets a task pause while it waits for I/O so another task can make progress. In a web server, that wait might be for a network connection, file, or database. An async runtime such as Tokio schedules the tasks; it can run multiple tasks on one operating-system thread without assigning a thread to every connection. section 10.1 section 10.3

Figure 10.2 Synchronous, asynchronous, and multithreading processing

Compare what happens to three tasks when one encounters a wait: synchronous processing stops at the wait, multithreading can use another operating-system thread, and async processing lets the runtime schedule another task. section 10.1

Two simulated file reads

Start with two independent reads delayed by four and two seconds. Calling them synchronously makes the delays add up to six seconds. A threaded version spawns and joins operating-system threads. The Tokio version introduces `#[tokio::main]`, `async fn`, `tokio::spawn`, `tokio::join!`, and `.await`. Watch the wait itself: the example’s `std::thread::sleep` blocks a thread, so declaring a function `async` does not make that sleep yield. section 10.2 section 10.3

An async function returns a future representing work that can produce a value later. Awaiting it allows that work to progress toward a result. At the executor level, `Future::poll` returns `Poll::Pending` or `Poll::Ready(value)`; a waker tells the executor when to poll a pending task again. A custom timer future demonstrates that pending-to-ready path, though application code can usually use Tokio or Actix Web APIs instead. section 10.3 section 10.5

Reach for async execution primarily when progress depends on I/O rather than CPU computation. Concurrency means tasks can make progress in an interleaved order; it does not necessarily mean they run simultaneously. section 10.3 section 10.6

How Rust Servers, Services, and Apps covers it

How deep it goes
Works from synchronous and threaded comparisons through Tokio tasks to future polling, wakers, and a custom timer future.
Examples
Uses two delayed file reads and a custom timer future to contrast execution models and show polling.
What it assumes
Assumes familiarity with Rust application code but develops the concurrent execution models through a small new program.
What you can do after
Build it from scratch with a custom future, or apply it via Tokio task APIs.
How it connects
Connects concurrency, multithreading, I/O-bound work, futures, executors, and wakers.

Also explained in

Compare how 28 books explain asynchronous programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.