[][src]Module tokio::time

Utilities for tracking time.

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

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 "100 ms have elapsed"

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");
}

A simple example using interval to execute a task every two seconds.

The difference between interval and delay_for is that an interval measures the time since the last tick, which means that .tick().await may wait for a shorter time than the duration specified for the interval if some time has passed between calls to .tick().await.

If the tick in the example below was replaced with delay_for, the task would only be executed once every three seconds, and not every two seconds.

use tokio::time;

async fn task_that_takes_a_second() {
    println!("hello");
    time::delay_for(time::Duration::from_secs(1)).await
}

#[tokio::main]
async fn main() {
    let mut interval = time::interval(time::Duration::from_secs(2));
    for _i in 0..5 {
        interval.tick().await;
        task_that_takes_a_second().await;
    }
}

Modules

delay_queue

A queue of delayed elements.

Structs

Delay

Future returned by delay_until and delay_for.

DelayQueue

A queue of delayed elements.

Duration

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

Elapsed

Error returned by Timeout.

Error

Errors encountered by the timer implementation.

Instant

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

Interval

Stream returned by interval and interval_at.

Throttle

Stream for the throttle function.

Timeout

Future returned by timeout and timeout_at.

Functions

advance

Advance time

delay_for

Waits until duration has elapsed.

delay_until

Waits until deadline is reached.

interval

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

interval_at

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

pause

Pause time

resume

Resume time

throttle

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

timeout

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

timeout_at

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