【Tokio】非同步週期任務
阿新 • • 發佈:2022-03-30
環境
- Time 2022-01-11
- Rust 1.57.0
- Tokio 1.15.0
概念
參考:https://docs.rs/tokio/latest/tokio/time/index.html
示例
interval
use std::{io, thread, time::Duration}; use tokio::time::{self, Instant}; #[tokio::main] async fn main() -> io::Result<()> { let start = Instant::now() + Duration::from_millis(50); let mut interval = interval_at(start, Duration::from_millis(200)); for _i in 0..5 { interval.tick().await; async { println!("{}", thread::current().name().unwrap()); println!("elapsed: {} ms", now.elapsed().as_millis()) } .await; } Ok(()) }
interval_at
use std::{io, thread}; use tokio::time::{self, Duration, Instant}; #[tokio::main] async fn main() -> io::Result<()> { let start = Instant::now() + Duration::from_secs(4); // 四秒後,每兩百毫秒執行一次 let mut interval = time::interval_at(start, Duration::from_millis(200)); for _i in 0..5 { interval.tick().await; async { println!("{}", thread::current().name().unwrap()); } .await; } Ok(()) }
總結
通過 interval
和 interval_at
來定義非同步的週期性任務。