其他分享
首页 > 其他分享> > 【Tokio】任务运行句柄

【Tokio】任务运行句柄

作者:互联网

环境

概念

参考:https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html

handle 方法返回一个可以执行任务的句柄。

示例

main.rs

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 方法可以获取一个执行任务的句柄。

附录

标签:Runtime,handle,句柄,Tokio,tokio,println,runtime,运行
来源: https://www.cnblogs.com/jiangbo44/p/15943598.html