基于tokio::time::interval的定时任务

use std::thread;
use tokio::runtime;
use tokio::time;
use std::time::{Duration, Instant};
use tokio::runtime::Runtime;
pub fn time_interval<F>(rt:&mut Runtime,millsecond:u64,f:F)
    where F:Fn(),
    F: Sync + 'static,
    F: Send + 'static,
{
    let millsecond =300;
    rt.spawn(async move {
        let mut interval = tokio::time::interval(Duration::from_millis(millsecond));
        interval.tick().await;
        let start = Instant::now();
        println!("time:{:?}", start);
        loop {
            interval.tick().await;
            f();
            println!("time:{:?}", start.elapsed());
        }
    });
}
原文地址:https://www.cnblogs.com/dhcn/p/12749938.html