mod*_*tos 5 concurrency asynchronous future rust
给定一个Futures的集合,比如说 a Vec<impl Future<..>>,我怎样才能同时阻塞和运行所有的Futures 直到第一个Future准备好?
我能找到的最接近的功能是select 宏(也可以在 Tokio 中使用)。不幸的是,它只适用于明确数量的Futures,而不是处理它们的集合。
Javascript 中有一个与此功能等效的功能,称为Promise.race。有没有办法在 Rust 中做到这一点?
或者也许有一种方法可以使用另一种模式(也许是通道)来实现此用例?
mod*_*tos 12
select_all我使用库中的函数futures找到了一个解决方案。
这是一个简单的示例,演示如何使用它来对期货集合进行竞赛:
use futures::future::select_all;
use futures::FutureExt;
use tokio::time::{delay_for, Duration};
async fn get_async_task(task_id: &str, seconds: u64) -> &'_ str {
println!("starting {}", task_id);
let duration = Duration::new(seconds, 0);
delay_for(duration).await;
println!("{} complete!", task_id);
task_id
}
#[tokio::main]
async fn main() {
let futures = vec![
// `select_all` expects the Futures iterable to implement UnPin, so we use `boxed` here to
// allocate on the heap:
// https://users.rust-lang.org/t/the-trait-unpin-is-not-implemented-for-genfuture-error-when-using-join-all/23612/3
// https://docs.rs/futures/0.3.5/futures/future/trait.FutureExt.html#method.boxed
get_async_task("task 1", 5).boxed(),
get_async_task("task 2", 4).boxed(),
get_async_task("task 3", 1).boxed(),
get_async_task("task 4", 2).boxed(),
get_async_task("task 5", 3).boxed(),
];
let (item_resolved, ready_future_index, _remaining_futures) =
select_all(futures).await;
assert_eq!("task 3", item_resolved);
assert_eq!(2, ready_future_index);
}
Run Code Online (Sandbox Code Playgroud)
以下是上述代码的链接: https://play.rust-lang.org/? version=stable&mode=debug&edition=2018&gist=f32b2ed404624c1b0abe284914f8658d
感谢@Herohtarselect_all在上面的评论中提出建议!