sleep

Function sleep 

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

Sleep for a specified duration

Examples found in repository?
examples/timeout_demo.rs (line 5)
4async fn slow_operation() -> i32 {
5    sleep(Duration::from_secs(5)).await;
6    42
7}
8
9async fn fast_operation() -> i32 {
10    sleep(Duration::from_millis(100)).await;
11    100
12}
More examples
Hide additional examples
examples/hello_world.rs (line 10)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async {
8        println!("Hello from avx Async!");
9
10        sleep(Duration::from_secs(1)).await;
11
12        println!("One second later...");
13    });
14}
examples/industry40_autoscale.rs (line 75)
72fn spawn_tasks(rt: &Runtime, count: usize, delay: Duration) {
73    for i in 0..count {
74        rt.spawn(async move {
75            avx_async::sleep(delay).await;
76            // Simulate CPU work
77            let mut sum = 0u64;
78            for j in 0..1000 {
79                sum = sum.wrapping_add(j);
80            }
81        });
82    }
83}
84
85async fn monitor_for_seconds(rt: &Runtime, seconds: u64) {
86    let end_time = std::time::Instant::now() + Duration::from_secs(seconds);
87
88    while std::time::Instant::now() < end_time {
89        avx_async::sleep(Duration::from_millis(500)).await;
90
91        let snapshot = rt.metrics().snapshot();
92        let active_tasks = snapshot.tasks_spawned - snapshot.tasks_completed;
93
94        println!(
95            "  [{}s] Tasks: {} active, {} queued | Threads: {} active, {} idle | TPS: {}",
96            (seconds - (end_time - std::time::Instant::now()).as_secs()),
97            active_tasks,
98            snapshot.queue_length,
99            snapshot.active_threads,
100            snapshot.idle_threads,
101            snapshot.tasks_per_second
102        );
103    }
104}
examples/industry40_tracing.rs (line 10)
4async fn process_order(ctx: &TraceContext, order_id: u64) {
5    let mut span = ctx.child_span("process_order");
6    span.set_attribute("order_id", order_id.to_string());
7    span.add_event("order_received");
8
9    // Simulate order validation
10    avx_async::sleep(Duration::from_millis(50)).await;
11    span.add_event("order_validated");
12
13    // Simulate payment processing
14    avx_async::sleep(Duration::from_millis(100)).await;
15    span.add_event("payment_processed");
16
17    // Simulate fulfillment
18    avx_async::sleep(Duration::from_millis(75)).await;
19    span.add_event("order_fulfilled");
20
21    println!("✅ Order {} processed", order_id);
22
23    let completed_span = span.end();
24    println!("   {}", completed_span);
25}
examples/parallel_tasks.rs (line 14)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async move {
8        println!("Spawning 100 concurrent tasks...");
9
10        let mut handles = vec![];
11
12        for i in 0..100 {
13            let handle = rt.spawn_with_handle(async move {
14                avx_async::sleep(Duration::from_millis(10)).await;
15                i * i
16            });
17            handles.push(handle);
18        }
19
20        println!("Waiting for all tasks to complete...");
21
22        let mut sum = 0;
23        for handle in handles {
24            if let Some(result) = handle.await_result().await {
25                sum += result;
26            }
27        }
28
29        println!("Sum of squares from 0 to 99: {}", sum);
30        println!("Active tasks: {}", rt.task_count());
31    });
32}
examples/channel_demo.rs (line 18)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async move {
8        let (tx, rx) = channel::bounded::<String>(10);
9
10        // Spawn producer task
11        rt.spawn({
12            let tx = tx.clone();
13            async move {
14                for i in 0..5 {
15                    let msg = format!("Message {}", i);
16                    println!("Sending: {}", msg);
17                    tx.send(msg).await.unwrap();
18                    avx_async::sleep(Duration::from_millis(500)).await;
19                }
20            }
21        });
22
23        // Spawn another producer
24        rt.spawn({
25            async move {
26                for i in 0..5 {
27                    let msg = format!("Urgent {}", i);
28                    println!("Sending: {}", msg);
29                    tx.send(msg).await.unwrap();
30                    avx_async::sleep(Duration::from_millis(300)).await;
31                }
32            }
33        });
34
35        // Receive messages
36        let mut count = 0;
37        while let Some(msg) = rx.recv().await {
38            println!("Received: {}", msg);
39            count += 1;
40            if count >= 10 {
41                break;
42            }
43        }
44
45        println!("All messages received!");
46    });
47}