当前位置: 首页> 科技> 互联网 > 嘉上营销_深圳装修设计生产厂家_360站长工具_网上国网app推广

嘉上营销_深圳装修设计生产厂家_360站长工具_网上国网app推广

时间:2025/7/14 5:46:59来源:https://blog.csdn.net/s912360101/article/details/146982457 浏览次数:0次
嘉上营销_深圳装修设计生产厂家_360站长工具_网上国网app推广
use tokio::{sync::oneshot, time::{sleep, Duration}};async fn check_for_one() {// This loop will continuously print "write" every second until interruptedloop {println!("write");sleep(Duration::from_secs(1)).await; // Non-blocking sleep in async context}
}#[tokio::main]
async fn main() {// Create a oneshot channellet (tx1, rx1) = oneshot::channel::<&str>();// Spawn a task that sends a message after 2 secondstokio::spawn(async move {sleep(Duration::from_secs(2)).await;    let _ = tx1.send("one");});// Use tokio::select! to wait for either the print task or the message on rx1tokio::select! {_ = check_for_one() => {// This branch will continuously print "write" every secondprintln!("check_for_one completed");}val = rx1 => {// This branch will be executed once the message is received from rx1match val {Ok(val) => println!("rx1 completed first with {:?}", val),Err(e) => println!("Failed to receive from rx1: {:?}", e),}}}println!("main thread exiting"); 
}

只要有一个异步任务完成,就会退出select! 。

关键字:嘉上营销_深圳装修设计生产厂家_360站长工具_网上国网app推广

版权声明:

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

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

责任编辑: