参考:https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html
handle
方法返回一个可以执行任务的句柄。
use std::{io, thread, time::Duration}; use tokio::runtime::Runtime; fn main() -> io::Result<()> { let runtime = Runtime::new()?; let handle = runtime.handle(); handle.spawn(async { println!("hello tokio"); println!("{}", thread::current().name().unwrap()); }); println!("{}", thread::current().name().unwrap()); runtime.shutdown_timeout(Duration::from_secs(4)); Ok(()) }
handle
方法可以获取一个执行任务的句柄。