Expand description
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 specificInstant
in time. -
Interval
is a stream yielding a value at a fixed period. It is initialized with aDuration
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
or a timer context must be setup explicitly. See the
tokio-timer
crate for more details on how to setup a timer
context.
Examples
Wait 100ms and print “Hello World!”
use tokio::prelude::*;
use tokio::timer::Delay;
use std::time::{Duration, Instant};
let when = Instant::now() + Duration::from_millis(100);
tokio::run({
Delay::new(when)
.map_err(|e| panic!("timer failed; err={:?}", e))
.and_then(|_| {
println!("Hello world!");
Ok(())
})
})
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::prelude::*;
use std::time::{Duration, Instant};
fn long_op() -> Box<Future<Item = (), Error = ()> + Send> {
// ...
}
tokio::run({
long_op()
.timeout(Duration::from_millis(300))
.map_err(|e| {
println!("operation timed out");
})
})
Modules
A queue of delayed elements.
Allows a future or stream to execute for a maximum amount of time.
Structs
A future that completes at a specified instant in time.
A queue of delayed elements.
Errors encountered by the timer implementation.
A stream representing notifications at fixed interval
Allows a Future
or Stream
to execute for a limited amount of time.