liveBook · Manning

Wake method

As explained in Rust Servers, Services, and Apps

The wake method on a Rust Waker notifies an asynchronous executor that the associated task should be scheduled for polling.

The wake() method on a Rust Waker tells the asynchronous executor that its associated task should be scheduled for polling again. A task that returns Poll::Pending stores a Waker handle in its Context rather than being polled continuously. section 10.4

In a custom Future implementation, the wake-and-poll cycle works like this:

  • Implement poll with a Pin<&mut Self> and mutable Context reference. section 10.4
  • Return Poll::Pending when the asynchronous computation is not ready. section 10.6
  • Call cx.waker().wake_by_ref() to wake the associated task; the executor then invokes poll again. section 10.4
  • Return Poll::Ready when the computation finishes; the Tokio runtime does not poll the completed future again. section 10.4

Self-waking custom future

A custom ReadFileFuture can print a message, call cx.waker().wake_by_ref(), and return Poll::Pending. This causes repeated polling until the implementation returns Poll::Ready with file contents. section 10.4

Figure 10.9 Writing a future with a waker component, step 2

Trace the custom future's waker flow from poll to wake and back to another poll. section 10.4

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains Waker storage in Context and executor callbacks, but stops short of implementing runtime internals.
Examples
Demonstrates wake_by_ref in ReadFileFuture and contrasts repeated pending polls with a final Poll::Ready result.
What it assumes
Assumes familiarity with Rust async tasks, futures, polling, and Tokio's executor model.
What you can do after
Apply it via Rust's Future API to reschedule custom futures from poll implementations.
How it connects
Connects Waker with Future, poll, Context, Poll::Pending, Poll::Ready, Tokio runtime, and asynchronous tasks.

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.