Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting the number of concurrent futures in join_all!()

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).

like image 474
porton Avatar asked Oct 23 '25 04:10

porton


1 Answers

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

like image 123
Frxstrem Avatar answered Oct 25 '25 22:10

Frxstrem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!