2 releases
0.1.1 | Mar 11, 2022 |
---|---|
0.1.0 | Mar 11, 2022 |
#1395 in Asynchronous
446 downloads per month
Used in groupcache
13KB
263 lines
Singleflight Async
Singleflight in async style.
Example
use singleflight_async::SingleFlight;
#[tokio::main]
async fn main() {
let group = SingleFlight::new();
let mut futures = Vec::new();
for _ in 0..10 {
futures.push(group.work("key", || async {
println!("will sleep to simulate async task");
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
println!("real task done");
"my-result"
}));
}
for fut in futures.into_iter() {
assert_eq!(fut.await, "my-result");
println!("task finished");
}
}
The output will be like:
will sleep to simulate async task
real task done
task finished
task finished
task finished
task finished
task finished
task finished
task finished
task finished
task finished
task finished
Dependencies
~2.4–9.5MB
~57K SLoC