Is there a way to schedule a task at a specific time or at intervals?

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

So that I can perform my function every 5 minutes or every day at 12 o’clock.

There is a TimerTask in Java, so I'm looking for something similar.

+4
source share
2 answers

You can use Timer::periodicto create a channel that sends a message at regular intervals, for example

use std::old_io::Timer;

let mut timer = Timer::new().unwrap();
let ticks = timer.periodic(Duration::minutes(5));
for _ in ticks.iter() {
    your_function();
}

Receiver::iter , , 5 , for . NB. , , , select!, , .

, ​​ , , . . Timer::periodic(Duration::days(1)) , . / .

+3

Rust:

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()
    }
}
+1

All Articles