-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.rs
More file actions
45 lines (39 loc) · 1.34 KB
/
Copy pathbasic.rs
File metadata and controls
45 lines (39 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use sprinter::Queue;
use std::fmt::Error;
use tokio::time::sleep;
#[tokio::main]
async fn main() {
println!("sprint start ...");
let start = tokio::time::Instant::now();
// Create a queue with concurrency of 2 and tick interval of 50ms
let queue: Queue<i32, Error, ()> = Queue::new(2);
// Define some async tasks
let task1 = || async {
println!("task1 start ...");
sleep(tokio::time::Duration::from_millis(250)).await;
println!("task1 done!");
Ok(1)
};
let task2 = || async {
println!("task2 start ...");
sleep(tokio::time::Duration::from_millis(50)).await;
println!("task2 done!");
Ok(2)
};
let task3 = || async {
println!("task3 start ...");
sleep(tokio::time::Duration::from_millis(50)).await;
println!("task3 done!");
Ok(3)
};
// Push tasks to the queue
queue.push(&"task1".to_string(), task1).await.unwrap();
queue.push(&"task2".to_string(), task2).await.unwrap();
queue.push(&"task3".to_string(), task3).await.unwrap();
// Signal that all tasks have been pushed
queue.set_push_done().await.unwrap();
// Wait for all tasks to complete and get results
queue.wait_for_tasks_done().await.unwrap();
let end = tokio::time::Instant::now();
println!("sprint done in {:?}", end - start);
}