rust-lang / futures-rs

Zero-cost asynchronous programming in Rust
https://rust-lang.github.io/futures-rs/
Apache License 2.0
5.4k stars 626 forks source link

then_concurrent / map_concurrent #2692

Closed FredrikNoren closed 1 year ago

FredrikNoren commented 1 year ago

Hi,

I'm sure I'm missing something, but I'm trying to find something like for_each_concurrent, but which will let me return a list of items too. then_concurrent doesn't exist; is there some other method that I'm missing? This is for a regular stream constructed with futures::stream::iter.

I.e., I'd like to do something like this:

let res = futures::stream::iter(files.iter())
  .then_concurrent(None, |file| tokio::spawn(async move { process(file) }).await.unwrap())
  .collect().await;

and have it process the files in parallel.

taiki-e commented 1 year ago

I think buffer_unordered/buffer + map/then works as such. See also #2609.