当前位置: 首页 > news >正文

rust学习-tokio::time

示例

use std::time::Duration;
use tokio::{task, time::interval};#[tokio::main]
async fn main() {let mut interval = interval(Duration::from_secs(1));let handle = task::spawn(async move {loop {interval.tick().await;println!("tick");}});handle.await.unwrap();
}

interval和sleep的区别

tick周期大于异步任务周期

use tokio::time;
use chrono::{DateTime, Local};async fn task_that_takes_a_second() {let now: DateTime<Local> = Local::now();println!("Current task time before is: {}", now.format("%Y-%m-%d %H:%M:%S"));time::sleep(time::Duration::from_secs(2)).await;let now: DateTime<Local> = Local::now();println!("Current task time after is: {}", now.format("%Y-%m-%d %H:%M:%S"));
}#[tokio::main]
async fn main() {let mut interval = time::interval(time::Duration::from_secs(3));for _i in 0..5 {let now: DateTime<Local> = Local::now();println!("Current main time before is: {}", now.format("%Y-%m-%d %H:%M:%S"));interval.tick().await;let now: DateTime<Local> = Local::now();println!("Current main time mid is: {}", now.format("%Y-%m-%d %H:%M:%S"));task_that_takes_a_second().await;let now: DateTime<Local> = Local::now();println!("Current main time after is: {}", now.format("%Y-%m-%d %H:%M:%S"));}
}
Current main time before is: 2023-08-11 13:46:48
Current main time mid is: 2023-08-11 13:46:48 // 第一次,立即触发
Current task time before is: 2023-08-11 13:46:48
Current task time after is: 2023-08-11 13:46:50
Current main time after is: 2023-08-11 13:46:50Current main time before is: 2023-08-11 13:46:50
Current main time mid is: 2023-08-11 13:46:51 // 距离上一次3秒
Current task time before is: 2023-08-11 13:46:51
Current task time after is: 2023-08-11 13:46:53
Current main time after is: 2023-08-11 13:46:53Current main time before is: 2023-08-11 13:46:53
Current main time mid is: 2023-08-11 13:46:54 // 距离上一次3秒
Current task time before is: 2023-08-11 13:46:54
Current task time after is: 2023-08-11 13:46:56
Current main time after is: 2023-08-11 13:46:56Current main time before is: 2023-08-11 13:46:56
Current main time mid is: 2023-08-11 13:46:57  // 距离上一次3秒
Current task time before is: 2023-08-11 13:46:57
Current task time after is: 2023-08-11 13:46:59
Current main time after is: 2023-08-11 13:46:59Current main time before is: 2023-08-11 13:46:59
Current main time mid is: 2023-08-11 13:47:00 // 距离上一次3秒
Current task time before is: 2023-08-11 13:47:00
Current task time after is: 2023-08-11 13:47:02
Current main time after is: 2023-08-11 13:47:02

tick周期小于异步任务周期

use tokio::time;
use chrono::{DateTime, Local};async fn task_that_takes_a_second() {let now: DateTime<Local> = Local::now();println!("Current task time before is: {}", now.format("%Y-%m-%d %H:%M:%S"));time::sleep(time::Duration::from_secs(5)).await;let now: DateTime<Local> = Local::now();println!("Current task time after is: {}", now.format("%Y-%m-%d %H:%M:%S"));
}#[tokio::main]
async fn main() {let mut interval = time::interval(time::Duration::from_secs(3));for _i in 0..5 {let now: DateTime<Local> = Local::now();println!("Current main time before is: {}", now.format("%Y-%m-%d %H:%M:%S"));interval.tick().await;let now: DateTime<Local> = Local::now();println!("Current main time mid is: {}", now.format("%Y-%m-%d %H:%M:%S"));task_that_takes_a_second().await;let now: DateTime<Local> = Local::now();println!("Current main time after is: {}", now.format("%Y-%m-%d %H:%M:%S"));}
}
Current main time before is: 2023-08-11 13:51:24
Current main time mid is: 2023-08-11 13:51:24
Current task time before is: 2023-08-11 13:51:24
Current task time after is: 2023-08-11 13:51:29
Current main time after is: 2023-08-11 13:51:29Current main time before is: 2023-08-11 13:51:29
Current main time mid is: 2023-08-11 13:51:29 // 举例上一次超过3秒
Current task time before is: 2023-08-11 13:51:29
Current task time after is: 2023-08-11 13:51:34
Current main time after is: 2023-08-11 13:51:34Current main time before is: 2023-08-11 13:51:34
Current main time mid is: 2023-08-11 13:51:34  // 举例上一次超过3秒
Current task time before is: 2023-08-11 13:51:34
Current task time after is: 2023-08-11 13:51:39
Current main time after is: 2023-08-11 13:51:39Current main time before is: 2023-08-11 13:51:39
Current main time mid is: 2023-08-11 13:51:39  // 举例上一次超过3秒
Current task time before is: 2023-08-11 13:51:39
Current task time after is: 2023-08-11 13:51:44
Current main time after is: 2023-08-11 13:51:44Current main time before is: 2023-08-11 13:51:44
Current main time mid is: 2023-08-11 13:51:44  // 举例上一次超过3秒
Current task time before is: 2023-08-11 13:51:44
Current task time after is: 2023-08-11 13:51:49
Current main time after is: 2023-08-11 13:51:49

timeout

use tokio::time::{timeout, Duration};
use tokio::time;
use chrono::{DateTime, Local};async fn long_future() {let now: DateTime<Local> = Local::now();println!("Current task time before is: {}", now.format("%Y-%m-%d %H:%M:%S"));time::sleep(time::Duration::from_secs(5)).await;let now: DateTime<Local> = Local::now();println!("Current task time after is: {}", now.format("%Y-%m-%d %H:%M:%S"));
}#[tokio::main]
async fn main() {for _i in 0..5 {let now: DateTime<Local> = Local::now();println!("Current main time before is: {}", now.format("%Y-%m-%d %H:%M:%S"));let res = timeout(Duration::from_secs(1), long_future()).await;let now: DateTime<Local> = Local::now();println!("Current main time after is: {}", now.format("%Y-%m-%d %H:%M:%S"));if res.is_err() {println!("operation timed out");}}
}

interval_at

pub fn interval_at(start: Instant, period: Duration) -> Interval
use tokio::time::{interval_at, Duration, Instant};
use chrono::{DateTime, Local};#[tokio::main]
async fn main() {let start = Instant::now() + Duration::from_secs(5);let mut interval = interval_at(start, Duration::from_secs(3)); // 不会立即开始let now: DateTime<Local> = Local::now();println!("Current task time now is: {}", now.format("%Y-%m-%d %H:%M:%S"));interval.tick().await; // ticks after 3slet now: DateTime<Local> = Local::now();println!("Current task time now is: {}", now.format("%Y-%m-%d %H:%M:%S"));interval.tick().await; // ticks after 3slet now: DateTime<Local> = Local::now();println!("Current task time now is: {}", now.format("%Y-%m-%d %H:%M:%S"));interval.tick().await; // ticks after 3slet now: DateTime<Local> = Local::now();println!("Current task time now is: {}", now.format("%Y-%m-%d %H:%M:%S"));
}
Current task time now is: 2023-08-11 19:34:30
Current task time now is: 2023-08-11 19:34:35
Current task time now is: 2023-08-11 19:34:38
Current task time now is: 2023-08-11 19:34:41

MissedTickBehavior

use tokio::time;
use chrono::{DateTime, Local};
use tokio::time::MissedTickBehavior;async fn task_that_takes_a_second() {let now: DateTime<Local> = Local::now();println!("Current task time before is: {}", now.format("%Y-%m-%d %H:%M:%S"));time::sleep(time::Duration::from_secs(5)).await;let now: DateTime<Local> = Local::now();println!("Current task time after is: {}", now.format("%Y-%m-%d %H:%M:%S"));
}#[tokio::main]
async fn main() {let mut interval = time::interval(time::Duration::from_secs(3));interval.set_missed_tick_behavior(MissedTickBehavior::Delay);for _i in 0..5 {let now: DateTime<Local> = Local::now();println!("Current main time before is: {}", now.format("%Y-%m-%d %H:%M:%S"));interval.tick().await;let now: DateTime<Local> = Local::now();println!("Current main time mid is: {}", now.format("%Y-%m-%d %H:%M:%S"));task_that_takes_a_second().await;let now: DateTime<Local> = Local::now();println!("Current main time after is: {}", now.format("%Y-%m-%d %H:%M:%S"));}
}

在 Rust 的 tokio 库中,MissedTickBehavior 是一个枚举类型,表示当 Interval 频率计时器在某个周期中错过某个间隔时如何处理。具体来说,它有以下三个变体:

  • Burst:表示如果错过计时间隔,则会立即执行多个周期,直到被重新赶上。
  • Delay:表示如果错过计时间隔,则在下一个可用的计时间隔时执行周期。
  • Skip:表示如果错过计时间隔,则跳过它并继续执行下一个计时间隔的周期。
    一般情况下, Burst 和 Delay 会导致执行速率加速,Skip 会导致执行速率降低但保证数据与频率同步。
#[tokio::main]
async fn main() {let mut interval_burst = time::interval(Duration::from_millis(5));interval_burst.set_missed_tick_behavior(time::MissedTickBehavior::Burst);let mut interval_delay = time::interval(Duration::from_millis(5));interval_delay.set_missed_tick_behavior(time::MissedTickBehavior::Delay);let mut count_burst = 0;let mut count_delay = 0;// 运行到20000次以上才会看出差异loop {select! {_ = interval_burst.tick() => {count_burst += 1;println!("Burst: tick #{}", count_burst);}_ = interval_delay.tick() => {count_delay += 1;println!("Delay: tick #{}", count_delay);}}}
}
http://www.lryc.cn/news/125163.html

相关文章:

  • Java 中 List 集合排序方法
  • prometheus监控k8s服务并告警到钉钉
  • Go和Java实现解释器模式
  • 域名配置HTTPS
  • 机械设计cad,ug编程设计,ug模具设计,SolidWorks模具设计
  • 嵌入式开发的学习与未来展望:借助STM32 HAL库开创创新之路
  • WPS-0DAY-20230809的分析和利用复现
  • MongoDB(三十九)
  • InnoDB引擎
  • CSS3中的var()函数
  • opencv图片换背景色
  • JAVA语言:什么是懒加载机制?
  • jupyter默认工作目录的更改
  • Flutter系列文章-Flutter UI进阶
  • Elasticsearch在部署时,对Linux的设置有哪些优化方法?
  • 【网络基础】应用层协议
  • 面试八股文Mysql:(1)事务实现的原理
  • Linux学习之sed多行模式
  • 【刷题笔记8.15】【链表相关】LeetCode:合并两个有序链表、反转链表
  • 神经网络基础-神经网络补充概念-11-向量化逻辑回归
  • openGauss学习笔记-40 openGauss 高级数据管理-锁
  • 勘探开发人工智能技术:机器学习(6)
  • 代理类型中的 HTTP、HTTPS 和 SOCKS 有什么区别?
  • 【STM32RT-Thread零基础入门】 3. PIN设备(GPIO)的使用
  • fiddler抓包工具的用法以及抓取手机报文定位bug
  • spring中时间格式化的两种方式
  • 【设计模式】原型模式
  • Matlab的Filter Designer工具设计二阶低通滤波器
  • 软件测试基础篇——LAMP环境搭建
  • 使用dom4j将xml转为String并去掉所有格式