How to make Rust execute all given futures (like join_all!) limiting to execute say 10 futures at once?
I need to download files from a big number of servers, but query no more than 10 servers simultaneously (to accurately measure their timeouts: if I query too many servers at once,, they timeout, even if the servers by themselves are fast).
You can do this with the futures crate by creating a stream of futures (for example by using futures::stream::iter) and call buffered or buffer_unordered to execute at most n futures in parallel.
use futures::prelude::*;
// create an iterator of futures to execute
let futures =
(0..50).map(|n| async move {
fetch(format!("https://server-{}.example.com", n)).await
});
// create a buffered stream that will execute up to 10 futures in parallel
// (without preserving the order of the results)
let stream = futures::stream::iter(futures).buffer_unordered(10);
// wait for all futures to complete
let results = stream.collect::<Vec<_>>().await;
Run example in playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With