Is there a way to schedule a task at a specific time or with an interval?

后端 未结 2 1362
我寻月下人不归
我寻月下人不归 2021-01-18 05:47

Is there a way to run a task in rust, a thread at best, at a specific time or in an interval again and again?

So that I can run my function every 5 minutes or every

2条回答
  •  渐次进展
    2021-01-18 06:31

    For the latest Rust nightly-version:

    use std::old_io::Timer;
    use std::time::Duration;
    
    let mut timer1 = Timer::new().unwrap();
    let mut timer2 = Timer::new().unwrap();
    let tick1 = timer1.periodic(Duration::seconds(1));
    let tick2 = timer2.periodic(Duration::seconds(3));
    
    loop {
        select! {
            _ = tick1.recv() => do_something1(),
            _ = tick2.recv() => do_something2()
        }
    }
    

提交回复
热议问题