liveBook · Manning

Task

As explained in Rust Servers, Services, and Apps

An asynchronous task is a schedulable unit of asynchronous computation managed by an asynchronous runtime.

An async server can spawn a task for each incoming HTTP request. The async runtime schedules those tasks for execution, allowing the server to handle multiple requests concurrently. A task is not the same as an operating-system thread: the runtime manages task scheduling, while the operating system schedules threads. section 10.1 section 10.6

Run two file reads

In src/main.rs, mark main with #[tokio::main] and async. Spawn each simulated file read with tokio::spawn(async { ... }), call its async function with .await inside the spawned block, then wait for both handles with tokio::join!(h1, h2). Without .await, the async file-read functions do not execute. The example uses Tokio 1 with its full feature set in Cargo.toml. section 10.2

A spawned task may contain one or more futures. When a future is polled, Poll::Ready supplies its value; Poll::Pending means it is not ready. A waker can notify the executor to poll the associated task again. In a custom timer example, the pending timer arranges a later wake-up, letting the runtime schedule another task in the meantime. A synchronous sleep inside an async function, by contrast, pauses the current thread. section 10.4 section 10.5

Figure 10.6 The Tokio executor

Shows how the Tokio executor, a spawned task, and its future relate. section 10.4

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains spawning, joining, and future polling through code, while deferring a full explanation of Pin.
Examples
Uses two simulated file reads and a custom timer to show concurrent scheduling and wake-up.
What it assumes
Builds on Rust functions and standard-library threads, then introduces Tokio as the needed async runtime.
What you can do after
Apply it via Tokio by spawning tasks, awaiting async functions, and joining their handles.
How it connects
Connects tasks to futures, executors, wakers, multithreading, and HTTP request handling.

Also explained in

Compare how 10 books explain task →

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.