Skip to main content

sleep

Function sleep 

Source
pub async fn sleep(dur: Duration)
Expand description

Sleeps for the specified amount of time.

This function might sleep for slightly longer than the specified duration but never less.

This function is an async version of std::thread::sleep.

See also: stream::interval.

ยงExamples

use std::time::Duration;

use async_std::task;

task::sleep(Duration::from_secs(1)).await;
Examples found in repository?
examples/supervisor_clear_send_later.rs (line 57)
43async fn main() -> Result<(), Box<dyn std::error::Error>> {
44    let service_supervisor = xactor::Supervisor::start(PingLater::default).await?;
45    let service_addr = service_supervisor.clone();
46
47    let supervisor_task = xactor::spawn(async {
48        service_supervisor.wait_for_stop().await;
49    });
50
51    let send_ping = async {
52        println!("  main  :: sending Ping");
53        service_addr.send(Ping("before halt")).unwrap();
54    };
55
56    let send_halt = async {
57        xactor::sleep(Duration::from_millis(1_000)).await;
58        println!("  main  :: sending Halt");
59        service_addr.send(Halt).unwrap();
60    };
61
62    let _ = futures::join!(supervisor_task, send_halt, send_ping);
63
64    Ok(())
65}
More examples
Hide additional examples
examples/supervisor_clear_interval.rs (line 77)
68async fn main() -> Result<(), Box<dyn std::error::Error>> {
69    let service_supervisor = xactor::Supervisor::start(PingTimer::default).await?;
70    let service_addr = service_supervisor.clone();
71
72    let supervisor_task = xactor::spawn(async {
73        service_supervisor.wait_for_stop().await;
74    });
75
76    let send_halt = async {
77        xactor::sleep(Duration::from_millis(5_200)).await;
78        println!("  main  :: sending Halt");
79        service_addr.send(Halt).unwrap();
80    };
81
82    let _ = futures::join!(supervisor_task, send_halt);
83    // run this to see that the interval is not properly stopped if the ctx is stopped
84    // futures::join!(supervisor_task, send_panic); // there is no panic recovery
85
86    Ok(())
87}