How to use async/await to complete concurrent tasks?

Hi, guys. I'm new to Rust.

I have a function do_something(start: u64, end: u64) -> Result<JobResult, JobError>, and I can spawn multiple do_something() calls and gather all the JobResult into a Vec<JobResult>.

I don't how to do it concurrently with async/await, I have no yet previous Rust async/await knowledge.

Is there anyone can help with this?

It depends on what your jobs are exactly doing and how. Do you mean that currently you are spawning threads and joining them? Are your jobs I/O bound or compute bound? Etc.

Do you mean that currently you are spawning threads and joining them?

Yes, spawning the job concurrently, and then joining the result if anyone is finished.

Are your jobs I/O bound or compute bound? Etc.

Mostly query from a memory DB(LMDB), I have no idea if it's IO bound or compute bound.

If these jobs are based around network I/O, then async/await is appropriate, and futures::join_all does this.

If these jobs do anything else, especially CPU-bound tasks, then it's better to use rayon:

My job mostly queries from a memory DB(LMDB), but I have no idea if it's IO bound or compute bound.

Since LMDB is an in-memory database, I'll assume the crate you use to access LMDB provides a synchronous interface (no Futures returned, no mentions of async in the documentation), hence your tasks should count as CPU-bound.

If you have an iterator of the (start, end) pairs that are the input to your do_something function, you can use the aforementioned rayon crate to create a parallel iterator (look for par_iter). You can then use the usual iterator methods to perform your processing (i.e. map and friends).

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.