如何等待rust中的异步函数调用列表? [英] How to wait for a list of async function calls in rust?

查看:234
本文介绍了如何等待rust中的异步函数调用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在rust中有一个async函数的列表,我想同时执行这些函数,然后等待所有这些函数完成.我现在拥有的工作代码是

I have a list of async functions in rust that I want to execute concurrently and then wait for all them to finish. The working code I have right now is

 async fn start_consumers(&self) {
    for consumer in &self.consumers {
        consumer.consume().await;
    }
}

由于功能是串行执行的,因此不太准确.我正在寻找类似join!的东西,但是它适用于动态矢量,使用它我应该能够写类似

This is not quite accurate as the functions are executed serially. I am looking for something like join!, but which works on a dynamic vector, Using which I should be able to write something like

 async fn start_consumers(&self) {
    let mut v = Vec::new();
    for consumer in &self.consumers {
        consumer.consume();
    }
    join!(v);
}

现在,join!仅支持元组.我正在寻找替代方案.类似于 Promise.all()在JavaScript中.

Right now join! supports only tuples. I am looking for an alternative for that. Something similar to Promise.all() in JavaScript.

推荐答案

我在同一天也问过类似的问题,但就我而言,我有一个Result包裹在Future中.因此,我不得不使用 try_join_all 来代替join_all >

I also asked a similar question on the same day, but in my case I had a Result wrapped in a Future. So instead of join_all I had to use try_join_all

这篇关于如何等待rust中的异步函数调用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆