欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > rust 同时处理多个异步任务,并在一个任务完成退出

rust 同时处理多个异步任务,并在一个任务完成退出

2025/4/18 9:32:12 来源:https://blog.csdn.net/s912360101/article/details/146982457  浏览:    关键词:rust 同时处理多个异步任务,并在一个任务完成退出
use std::thread;
use tokio::{sync::mpsc,time::{sleep, Duration},
};async fn check_for_one() {// 该函数会每秒打印一次 "write"loop {println!("write");sleep(Duration::from_secs(1)).await;}
}async fn start_print_task() -> Result<(), ()> {// 在新线程中运行 Tokio 运行时thread::spawn(move || {// 创建一个新的 Tokio 运行时let rt = tokio::runtime::Runtime::new().unwrap_or_else(|e| panic!("Failed to create Tokio runtime: {}", e));// 使用 Tokio 运行时执行异步任务rt.block_on(async move {check_for_one().await;});});// 返回一个已完成的 Future,方便配合 select! 使用sleep(Duration::from_secs(1)).await;Ok::<(), ()>(())
}#[tokio::main]
async fn main() {// 创建一个只发送“信号”的通道,类型为 ()let (tx, mut rx) = mpsc::channel::<()>(1);// 启动打印任务,返回一个 Futurelet print_task = start_print_task();// 启动另一个异步任务,2 秒后向通道发送“信号”tokio::spawn(async move {sleep(Duration::from_secs(2)).await;let _ = tx.send(()).await;});// 使用 tokio::select! 监听tokio::select! {val = rx.recv() => {match val {Some(_) => println!("rx1 completed first with signal"),None => println!("rx1 channel closed"),}}_ = print_task => {println!("start_print_task completed");}}println!("main thread exiting");
}

使用select!宏 ,来完成 只要有一个异步任务完成,就会退出异步监听。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词