How to wait for a list of async function calls in rust?

前端 未结 2 1027
一个人的身影
一个人的身影 2020-12-22 04:01

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



        
2条回答
  •  悲哀的现实
    2020-12-22 04:43

    So after some searching I found that rust futures has a function called join_all which allows for waiting on a collection of futures.

     use futures::future::join_all;
     ....
    
     async fn start_consumers(&self) {
        let mut v = Vec::new();
        for consumer in &self.consumers {
            v.push(consumer.consume());
        }
        join_all(v).await;
     }
    

提交回复
热议问题