-
Notifications
You must be signed in to change notification settings - Fork 6
/
basic.rs
43 lines (34 loc) · 1.14 KB
/
basic.rs
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
use std::time::Duration;
use futures::stream::{self, StreamExt};
use rand::thread_rng;
use rand::Rng;
use tracing::info;
use tracing::instrument;
use tracing_indicatif::IndicatifLayer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
#[instrument]
async fn do_work(val: u64) -> u64 {
let sleep_time = thread_rng().gen_range(Duration::from_millis(250)..Duration::from_millis(500));
tokio::time::sleep(sleep_time).await;
info!("doing work for val: {}", val);
let sleep_time =
thread_rng().gen_range(Duration::from_millis(500)..Duration::from_millis(1000));
tokio::time::sleep(sleep_time).await;
val + 1
}
#[tokio::main]
async fn main() {
let indicatif_layer = IndicatifLayer::new();
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_writer(indicatif_layer.get_stderr_writer()))
.with(indicatif_layer)
.init();
let res: u64 = stream::iter((0..20).map(|val| do_work(val)))
.buffer_unordered(5)
.collect::<Vec<u64>>()
.await
.into_iter()
.sum();
println!("final result: {}", res);
}