liveBook · Manning

Executor

As explained in Rust Servers, Services, and Apps

The Tokio executor is the Tokio runtime component that drives futures toward completion by polling them when they can make progress.

The Tokio executor is the part of the async runtime that drives futures toward completion. Creating a future does not run its computation: the executor must poll it. In a Tokio program, an async task can be spawned with `tokio::spawn`, awaited, and joined with other tasks. section 10.3

A future’s `poll` method returns `Poll::Ready(value)` when its result is available or `Poll::Pending` when it cannot finish yet. A pending task uses the `Waker` from its polling context to notify the runtime when it can make progress; the executor can then poll it again. Returning `Pending` without arranging a wake leaves the task unfinished, while waking it on every poll can produce repeated polling without progress. section 10.4 section 10.6

An expiration timer

A custom `Future` stores an expiration time. Its `poll` method returns `Ready` once that time has passed. Otherwise, it arranges for a sleeping thread to call the task’s waker and returns `Pending`. That lets the executor run another task before polling the timer again. The contrasting simulated timer calls `std::thread::sleep()` within its task. section 10.5

impl Future for ReadFileFuture {
    type Output = String;
 
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) ->
      Poll<Self::Output> {                                  #1
        println!("Tokio! Stop polling me");
        cx.waker().wake_by_ref();                           #2
        Poll::Pending
    }
}

This `poll` implementation calls `wake_by_ref()` and immediately returns `Pending`; its repeated output shows why waking a task must be tied to actual progress. section 10.4

How Rust Servers, Services, and Apps covers it

How deep it goes
Works through polling, pending results, waking, and a custom timer; treats pinning as a requirement rather than explaining it fully.
Examples
Uses spawned file-reading tasks, a future that stays pending, and an expiration timer that wakes its task.
What it assumes
Assumes Rust async and Tokio task usage while introducing Future::poll; defers a full explanation of Pin.
What you can do after
Evaluate/understand it: trace why a Tokio task remains pending, gets polled again, or completes.
How it connects
Connects the Future trait, Poll, Waker, async runtime, and task scheduling.

Also explained in

Compare how 3 books explain executor →

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.