8 Async Rust

 

This chapter covers

  • Thinking asynchronously: an overview of async programming
  • Exploring Rust’s async runtimes
  • Handling async task results with futures
  • Mixing sync and async
  • Using the async and .await features
  • Managing concurrency and parallelism with async
  • Implementing an async observer
  • Understanding when to avoid using async
  • Tracing and debugging async code
  • Dealing with async when testing

Concurrency is an important concept in computing, and it’s one of the greatest force multipliers of computers. Concurrency allows us to process inputs and outputs—such as data, network connections, or peripherals—faster than we might be able to without concurrency. And it’s not always about speed but also latency, overhead, and system complexity. We can run thousands or millions of tasks concurrently, as illustrated in figure 8.1, because concurrent tasks tend to be relatively lightweight. We can create, destroy, and manage many concurrent tasks with very little overhead.

Figure 8.1 Tasks executing concurrently within the same thread
CH08_F01_Matthews

Asynchronous programming uses concurrency to take advantage of idle processing time between tasks. Some kinds of tasks, such as I/O, are much slower than ordinary CPU instructions, and after a slow task is started, we can set it aside to work on other tasks while waiting for the slow task to be completed.

8.1 Runtimes

8.2 Thinking asynchronously

8.3 Futures: Handling async task results

8.3.1 Defining a runtime with #[tokio::main]

8.4 The async and .await keywords: When and where to use them

8.5 Concurrency and parallelism with async

8.6 Implementing an async observer

8.7 Mixing sync and async

8.8 When to avoid using async

sitemap