| Crates.io | singleflight-async |
| lib.rs | singleflight-async |
| version | 0.2.0 |
| created_at | 2022-03-11 08:42:56.660152+00 |
| updated_at | 2024-11-06 07:04:07.327895+00 |
| description | Singleflight in async style. |
| homepage | |
| repository | https://github.com/ihciah/singleflight-async |
| max_upload_size | |
| id | 548089 |
| size | 22,880 |
Singleflight in async style.
Send/Sync, or 'static.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"
}));
}
let begin = std::time::Instant::now();
for fut in futures.into_iter() {
assert_eq!(fut.await, "my-result");
println!("task finished");
}
println!("time elapsed: {:?}", begin.elapsed());
}
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
time elapsed: 100.901321ms