简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM