gpt4 book ai didi

timer - Rust 是否有 Python 的 threading.Timer 的等价物?

转载 作者:行者123 更新时间:2023-11-29 07:49:22 25 4
gpt4 key购买 nike

我正在寻找一个使用线程的计时器,而不是普通的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

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);
}

关于timer - Rust 是否有 Python 的 threading.Timer 的等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37009480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com