如何在循环中生成异步方法?

Dje*_*ent 3 rust async-await rust-tokio

我有有一个对象的矢量resolve()的方法,它使用reqwest来查询外部web API。在resolve()每个对象上调用该方法后,我想打印每个请求的结果。

这是我编译和工作的半异步代码(但不是真正异步的):

for mut item in items {
    item.resolve().await;

    item.print_result();
}
Run Code Online (Sandbox Code Playgroud)

我试图用来tokio::join!产生所有异步调用并等待它们完成,但我可能做错了什么:

tokio::join!(items.iter_mut().for_each(|item| item.resolve()));
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

error[E0308]: mismatched types
  --> src\main.rs:25:51
   |
25 |     tokio::join!(items.iter_mut().for_each(|item| item.resolve()));
   |                                                   ^^^^^^^^^^^^^^ expected `()`, found opaque type
   | 
  ::: src\redirect_definition.rs:32:37
   |
32 |     pub async fn resolve(&mut self) {
   |                                     - the `Output` of this `async fn`'s found opaque type
   |
   = note: expected unit type `()`
            found opaque type `impl std::future::Future`
Run Code Online (Sandbox Code Playgroud)

如何一次调用resolve()所有实例的方法?


这段代码反映了答案——现在我正在处理我不太理解的借用检查器错误——我应该用 注释我的一些变量'static吗?

let mut items = get_from_csv(path);

let tasks: Vec<_> = items
    .iter_mut()
    .map(|item| tokio::spawn(item.resolve()))
    .collect();

for task in tasks {
    task.await;
}

for item in items {
    item.print_result();
}
Run Code Online (Sandbox Code Playgroud)
error[E0597]: `items` does not live long enough
  --> src\main.rs:18:25
   |
18 |       let tasks: Vec<_> = items
   |                           -^^^^
   |                           |
   |  _________________________borrowed value does not live long enough
   | |
19 | |         .iter_mut()
   | |___________________- argument requires that `items` is borrowed for `'static`
...
31 |   }
   |   - `items` dropped here while still borrowed

error[E0505]: cannot move out of `items` because it is borrowed
  --> src\main.rs:27:17
   |
18 |       let tasks: Vec<_> = items
   |                           -----
   |                           |
   |  _________________________borrow of `items` occurs here
   | |
19 | |         .iter_mut()
   | |___________________- argument requires that `items` is borrowed for `'static`
...
27 |       for item in items {
   |                   ^^^^^ move out of `items` occurs here
Run Code Online (Sandbox Code Playgroud)

use*_*342 6

既然你想在并行期货的await,就可以酿出他们到并行运行的各个任务。由于它们彼此独立运行,并且与产生它们的线程无关,因此您可以以任何顺序等待它们的句柄。

理想情况下,你会写这样的东西:

// spawn tasks that run in parallel
let tasks: Vec<_> = items
    .iter_mut()
    .map(|item| tokio::spawn(item.resolve()))
    .collect();
// now await them to get the resolve's to complete
for task in tasks {
    task.await.unwrap();
}
// and we're done
for item in &items {
    item.print_result();
}
Run Code Online (Sandbox Code Playgroud)

但这将被借用检查器拒绝,因为 返回的未来item.resolve()持有对 的借用引用item。引用被传递给tokio::spawn()另一个线程,并且编译器无法证明它item会比那个线程存活得更久。(当您想将本地数据的引用发送到线程时,会遇到同样的问题。)

对此有几种可能的解决方案;我觉得最优雅的方法是项目移动到传递给 的异步闭包中tokio::spawn(),并在任务完成后将它们交还给您。基本上,您使用items向量来创建任务并立即从等待的结果中重新构建它:

// note the use of `into_iter()` to consume `items`
let tasks: Vec<_> = items
    .into_iter()
    .map(|mut item| {
        tokio::spawn(async {
            item.resolve().await;
            item
        })
    })
    .collect();
// await the tasks for resolve's to complete and give back our items
let mut items = vec![];
for task in tasks {
    items.push(task.await.unwrap());
}
// verify that we've got the results
for item in &items {
    item.print_result();
}
Run Code Online (Sandbox Code Playgroud)

操场上运行的代码。

请注意,futures板条箱包含一个join_all与您需要的功能类似的功能,除了它轮询单个期货而不确保它们并行运行。我们可以编写一个join_parallel使用的泛型join_all,但也用于tokio::spawn获得并行执行:

async fn join_parallel<T: Send + 'static>(
    futs: impl IntoIterator<Item = impl Future<Output = T> + Send + 'static>,
) -> Vec<T> {
    let tasks: Vec<_> = futs.into_iter().map(tokio::spawn).collect();
    // unwrap the Result because it is introduced by tokio::spawn()
    // and isn't something our caller can handle
    futures::future::join_all(tasks)
        .await
        .into_iter()
        .map(Result::unwrap)
        .collect()
}
Run Code Online (Sandbox Code Playgroud)

使用此函数,回答问题所需的代码可归结为:

let items = join_parallel(items.into_iter().map(|mut item| async {
    item.resolve().await;
    item
})).await;
for item in &items {
    item.print_result();
}
Run Code Online (Sandbox Code Playgroud)

同样,playground 中的可运行代码。

  • 感谢您的耐心等待。现在我更了解正在发生的事情了。该解决方案非常聪明,我喜欢这样一个事实:我不必更改“resolve()”方法的返回类型,因为它已由匿名函数处理。 (2认同)