Using Rayon with Tokio?

I couldn't find much information about handling multi-threaded processes in an async environment.

Anything to watch out for?

Taking an example from Rayon's docs:

use std::sync::mpsc::channel;
use rayon::prelude::*;

let (sender, receiver) = channel();

(0..5).into_par_iter().for_each_with(sender, |s, x| s.send(x).unwrap());

let mut res: Vec<_> = receiver.iter().collect();


assert_eq!(&res[..], &[0, 1, 2, 3, 4])

Is that the recommended way to iterate through elements in an async block? Or would it better to use Tokio's channel instead? - whatever the answer is, I'd appreciate if you could also tell me why.

There isn't anything async in your example, so I'm not sure what the connection is. Generally, it does not make sense to send messages on channels with rayon — it isn't going to be faster than doing so in a loop. You also would not use the std::sync::mpsc channel in async code, but instead use an async channel such as tokio::sync::mpsc.

There are some examples of using rayon in Tokio in this article.

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.