#tokio #timeout #async

async-timeouts

An instrument to start async tasks after timeouts

1 unstable release

new 0.1.1 Apr 8, 2025

#733 in Asynchronous

MIT license

8KB
51 lines

Async Timeouts Helper

It is a simple instrument to delay execution of an async task with the possibility to stop timer or reset it with another time value.

It is convinient to use this crate with Notify or different channels (i.e. async_channel).

Example

use std::time::Duration;
use async_timeout::Timeout;
use tokio::sync::Notify;

#[tokio::main]
async fn main() {
    let event = Arc::new(Notify::new());
    let timer = Instant::now();
    {
        let event = event.clone();
        // Let's notify our event after 3 seconds
        Timeout::set(Duration::from_secs(3), async move {
            event.notify_one();
        }).await;
    }
    event.notified().await;
    println!("{} seconds elapsed", timer.elapsed().as_secs());
}

Take a look at one more complex example.

Dependencies

~3.5–9MB
~72K SLoC