参考:https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html
除了可以使用线程池运行时,还可以直接在当前线程运行任务,使用单线程。
use std::{io, thread, time::Duration}; use tokio::runtime::Builder; fn main() -> io::Result<()> { let runtime = Builder::new_current_thread().build()?; runtime.block_on(async { println!("hello tokio"); println!("{}", thread::current().name().unwrap()); }); println!("{}", thread::current().name().unwrap()); runtime.shutdown_timeout(Duration::from_secs(4)); Ok(()) }
新建了一个单线程的异步运行时。