AFAIK there's no special threadpool crate for std futures (like there was futures-cpupool for v0.1), and the current practice is to use a tokio threaded runtime and block_in_place
on it.
I want to separate my CPU-heavy async threadpool from my normal I/O-bound threadpool, so that heavy tasks can't starve network or cause latency. I've tried simply creating two runtimes, but tokio is incredibly annoying with the error:
Cannot start a runtime from within a runtime. This happens because a function (like
block_on
) attempted to block the current thread while the thread is being used to drive asynchronous tasks.
AFAIK I'm not starting a runtime from within a runtime (both runtimes are created in main
, which isn't async), and I'm not blocking inside block_on
— the inner .spawn
is non-blocking.
So why tokio doesn't like this?
fn main() {
let mut rt = tokio::runtime::Builder::new().threaded_scheduler().build().unwrap();
let second_rt = tokio::runtime::Builder::new().threaded_scheduler().build().unwrap();
rt.block_on(rt.spawn(async move {
second_rt.spawn(async {}).await
}))
.unwrap().unwrap();
}
How can I use two runtimes together, without running into "Cannot start a runtime from within a runtime" error?