[][src]Module tokio::time

This is supported on feature="time" only.

Utilities for tracking time.

This module provides a number of types for executing code after a set period of time.

  • Delay is a future that does no work and completes at a specific Instant in time.

  • Interval is a stream yielding a value at a fixed period. It is initialized with a Duration and repeatedly yields each time the duration elapses.

  • Timeout: Wraps a future or stream, setting an upper bound to the amount of time it is allowed to execute. If the future or stream does not complete in time, then it is canceled and an error is returned.

  • DelayQueue: A queue where items are returned once the requested delay has expired.

These types are sufficient for handling a large number of scenarios involving time.

These types must be used from within the context of the Runtime.

Examples

Wait 100ms and print "Hello World!"

use tokio::time::delay_for;

use std::time::Duration;


#[tokio::main]
async fn main() {
    delay_for(Duration::from_millis(100)).await;
    println!("100 ms have elapsed");
}

Require that an operation takes no more than 300ms. Note that this uses the timeout function on the FutureExt trait. This trait is included in the prelude.

use tokio::time::{timeout, Duration};

async fn long_future() {
    // do work here
}

let res = timeout(Duration::from_secs(1), long_future()).await;

if res.is_err() {
    println!("operation timed out");
}

Modules

delay_queuefeature="time"

A queue of delayed elements.

Structs

Delayfeature="time"

Future returned by delay_until and delay_for.

DelayQueuefeature="time"

A queue of delayed elements.

Durationfeature="time"

A Duration type to represent a span of time, typically used for system timeouts.

Elapsedfeature="time"

Error returned by Timeout.

Errorfeature="time"

Errors encountered by the timer implementation.

Instantfeature="time"

A measurement of the system clock, useful for talking to external entities like the file system or other processes.

Intervalfeature="time"

Stream returned by interval and interval_at.

Throttlefeature="time"

Stream for the throttle function.

Timeoutfeature="time"

Future returned by timeout and timeout_at.

Functions

advancefeature="time" and feature="test-util"

Advance time

delay_forfeature="time"

Waits until duration has elapsed.

delay_untilfeature="time"

Waits until deadline is reached.

intervalfeature="time"

Creates new Interval that yields with interval of duration. The first tick completes immediately.

interval_atfeature="time"

Creates new Interval that yields with interval of period with the first tick completing at at.

pausefeature="time" and feature="test-util"

Pause time

resumefeature="time" and feature="test-util"

Resume time

throttlefeature="time"

Slows down a stream by enforcing a delay between items. They will be produced not more often than the specified interval.

timeoutfeature="time"

Require a Future to complete before the specified duration has elapsed.

timeout_atfeature="time"

Require a Future to complete before the specified instant in time.