15 releases
| 0.6.1 | Aug 27, 2025 |
|---|---|
| 0.5.3 | Aug 25, 2025 |
| 0.5.2 | Nov 30, 2022 |
| 0.5.0 | Jun 30, 2022 |
| 0.3.0 | Feb 23, 2021 |
#335 in Asynchronous
9,812 downloads per month
Used in 5 crates
(2 directly)
23KB
483 lines
Async singleflight
Documentation: async_singleflight.
lib.rs:
A singleflight implementation for tokio.
Inspired by singleflight.
Examples
use futures::future::join_all;
use std::sync::Arc;
use std::time::Duration;
use async_singleflight::DefaultGroup;
const RES: usize = 7;
async fn expensive_fn() -> Result<usize, ()> {
tokio::time::sleep(Duration::new(1, 500)).await;
Ok(RES)
}
#[tokio::main]
async fn main() {
let g = Arc::new(DefaultGroup::<usize>::new());
let mut handlers = Vec::new();
for _ in 0..10 {
let g = g.clone();
handlers.push(tokio::spawn(async move {
let res = g.work("key", expensive_fn()).await;
let r = res.unwrap();
println!("{}", r);
}));
}
join_all(handlers).await;
}
Dependencies
~2–3MB
~49K SLoC