2 ms·
This has not been my experience at all. Delegating compute-intensive tasks to rayon inside a tokio runtime is not particularly hard (assuming you can pipeline t
by thinkharderdev 3y ago
This has not been my experience at all. Delegating compute-intensive tasks to rayon inside a tokio runtime is not particularly hard (assuming you can pipeline things to separate IO and compute effectively).
A pattern that has worked quite well for me is to use
```
struct ComputeTask {
some_state: SomeState,
completion: Sender<Output>,
}
impl ComputeTask {
fn run(self) {
.. do you compute-intensive stuff
self.sender.send(output);
}
}
async fn do_stuff() -> Result<Output> {
let (tx,rx) = tokio::sync::oneshot::channel();
let task = ComputeTask { .., tx }
rayon::spawn(move || task.run());
rx.await
}
```