How to run async multiple functions in parallel?

Hi all
How to run multiple async functions in parallel in different threads using tokio.
Thanks

You can spawn them: spawn in tokio::task - Rust

actually its not working

tokio::spawn
tokio::task::spawn

both are running currently & not in parallel.

How I notice it?

I have multiple corn jobs with 1 minute delay.

And there is a http server which accept request. So when the 1 minute time over then only server is responding so the server is stuck whole minute.

To get parallelism (not just concurrency) you have to use the multithreaded runtime.

But please say more about the cron job and how this is related to the problem you're experiencing. If you can post some code for the different pieces it would help to understand the problem.

I'm guessing you're using std::thread::sleep instead of tokio::time::sleep. std::thread::sleep blocks the runtime, which will cause unpredictable behavior. This happens whenever you use a long-blocking function.

There should be no difference in behavior between same-thread and multithread tokio runtimes except for performance.

1 Like

My bad
I used #[tokio::main(flavor = "current_thread")] instead of #[tokio::main]
Sorry
Thanks for your response guys.

I'm just curious about why you needed real parallelism to solve the problem. Is it because you're doing a very large amount of of pure computation (with no IO) in one of the tasks? Or perhaps you're using blocking IO or some other blocking call (which would probably be a mistake).

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.