Rust 是否有等效于 Python 的 threading.Timer? [英] Does Rust have an equivalent of Python's threading.Timer?
本文介绍了Rust 是否有等效于 Python 的 threading.Timer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个使用线程的计时器,而不是简单的time.sleep
:
I'm looking for a timer which uses threads, not plain time.sleep
:
from threading import Timer
def x():
print "hello"
t = Timer(2.0, x)
t.start()
t = Timer(2.0, x)
t.start()
推荐答案
您可以使用 timer crate
You can use the timer crate
extern crate timer;
extern crate chrono;
use timer::Timer;
use chrono::Duration;
use std::thread;
fn x() {
println!("hello");
}
fn main() {
let timer = Timer::new();
let guard = timer.schedule_repeating(Duration::seconds(2), x);
// give some time so we can see hello printed
// you can execute any code here
thread::sleep(::std::time::Duration::new(10, 0));
// stop repeating
drop(guard);
}
这篇关于Rust 是否有等效于 Python 的 threading.Timer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文