简体   繁体   中英

How do I limit the Tokio threadpool to a certain number of native threads?

What's the correct way of limiting the Tokio (v 0.1.11) threadpool to n OS native threads, where n is an arbitrary number, preferably configurable at runtime?

As far as I can tell, it's possible to use Tokio in single threaded mode using using tokio_current_thread::block_on_all instead of tokio::run and tokio_current_thread::spawn instead of tokio::spawn .

I'd like a similar solution but for n >= 1 .

You can build a Tokio Runtime object using tokio::runtime::Builder . The builder offers a core_threads() method that can be used to configure the number of threads, eg

let mut rt = runtime::Builder::new()
    .core_threads(4)
    .build()
    .unwrap();

You can then use rt.spawn(some_future) to run a future on this runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM