3

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

porton
  • 5,214
  • 11
  • 47
  • 95
  • 3
    I would say use a thread pool. But if you want to stick with async how about using a semaphore flagged up 10 times initially. [Here's a crate](https://docs.rs/async-semaphore/latest/async_semaphore/). [And another](https://github.com/nathdobson/async-weighted-semaphore) You'll need some special async semaphore of course not a normal thread based done. – nlta Jan 26 '22 at 23:16

1 Answers1

12

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

Frxstrem
  • 38,761
  • 9
  • 79
  • 119