Fabian Wunsch
Fabian Wunsch

Reputation: 23

Is there a way to poll several futures simultaniously in rust async

I'm trying to execute several sqlx queries in parallel given by a iterator. This is probably the closest I've got so far.

let mut futures = HahshMap::new() // placeholder, filled HashMap in reality
    .iter()
    .map(async move |(_, item)| -> Result<(), sqlx::Error> {
        let result = sqlx::query_file_as!(
            // omitted
        )
            .fetch_one(&pool)
            .await?;
        channel.send(Enum::Event(result)).ignore();
        Ok(())
    })
    .clollect();
futures::future::join_all(futures);

All queries and sends are independent from each other, so if one of them fails, the others should still get processed. Futthermore the current async closure is not possible like this.

Upvotes: 1

Views: 560

Answers (1)

Lucretiel
Lucretiel

Reputation: 3344

Rust doesn't yet have async closures. You instead need to have the closure return an async block:

move |(_, item)| async move { ... }

Additionally, make sure you .await the future returned by join_all in order to ensure the individual tasks are actually polled.

Upvotes: 1

Related Questions