Geo*_*rge 2 multithreading asynchronous rust mio rust-tokio
将 Tokio (v 0.1.11)n线程池限制为OS 本机线程的正确方法是什么,其中n是任意数量,最好在运行时配置?
据我所知,可以使用 usingtokio_current_thread::block_on_all代替tokio::run和tokio_current_thread::spawn代替在单线程模式下使用 Tokiotokio::spawn。
我想要一个类似的解决方案,但对于n >= 1.
您可以Runtime使用tokio::runtime::Builder. builder 提供了一种core_threads()方法,可以用来配置线程的数量,例如
let mut rt = runtime::Builder::new()
.core_threads(4)
.build()
.unwrap();
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用rt.spawn(some_future)在此运行时上运行未来。