Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a list of async functions 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;
    }
}

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. With that 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);
}

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

like image 511
Sam Radhakrishnan Avatar asked Aug 09 '20 13:08

Sam Radhakrishnan


People also ask

How do I make async call wait?

An alternative way to wait for all promises to be done, you have to use async and await keywords, just add async keyword before function checkPhotos(... and ads await keyword before Promise. all... , that's will tell js engine that you need to run the code below await line when you got the response.

Does Rust have async await?

async / . await is Rust's built-in tool for writing asynchronous functions that look like synchronous code. async transforms a block of code into a state machine that implements a trait called Future .

How does async await work in Rust?

async / . await are special pieces of Rust syntax that make it possible to yield control of the current thread rather than blocking, allowing other code to make progress while waiting on an operation to complete.

How long does async await wait?

The keyword await is used to wait for a Promise. It can only be used inside an async function. This keyword makes JavaScript wait until that promise settles and returns its result. Here is an example with a promise that resolves in 2 seconds.

What happens when you call an async function in rust?

But in Rust, invoking an async function does not do any scheduling, and here the main thing you’ll notice is that futures feel “lazy”: they won’t do anything until you await them inside the async block. After awaiting the Future then the Future executor comes into the picture.

How do you use async FN and await?

Then, we create two examples of asynchronous functions that use async fn and await. The first function is retrieve_val, whose purpose is to return the value numeric value five after ten seconds. The other asynchronous function is pre_retrieval, which calls the retrieval_val function.

What is the use of await in rust?

In general, async / await lets you write code that avoids "callback hell", in favor of a linear style similar to blocking code while still letting other tasks progress during awaits. Any minimal async / await code in Rust is made up of at least the following pieces: A Future with an internal state machine

Is rust asynchronous with zero-cost futures?

As you all know the async–await syntax hits stable Rust, as part of the 1.39.0 release. So let’s leverage this stabilized feature to make our code-bases Asynchronous with Zero-cost futures.


2 Answers

The futures crate has a join_all function 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;
 }
like image 61
Sam Radhakrishnan Avatar answered Oct 19 '22 02:10

Sam Radhakrishnan


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

like image 5
WBuck Avatar answered Oct 19 '22 01:10

WBuck