Go 的范围 time.Tick 相当于多少?
What is the equivalent of Go's range time.Tick?
我是新手,目前正在学习 Rust,来自 Go。我如何实现像长并发轮询这样的东西?
// StartGettingWeather initialize weather getter and setter
func StartGettingWeather() {
// start looping
for i := range time.Tick(time.Second * time.Duration(delay)) {
_ = i
loopCounter++
fmt.Println(time.Now().Format(time.RFC850), " counter: ", loopCounter)
mainWeatherGetter()
}
}
我将把这个函数称为 go StartGettingWeather()
Rust 线程是 OS 线程,它们使用 OS 调度程序,因此您可以使用 thread::sleep_ms
:
来模拟它
use std::thread;
fn start_getting_weather() {
let mut loop_counter = 0;
loop {
loop_counter += 1;
println!("counter: {}", loop_counter);
main_weather_getter();
thread::sleep_ms(delay);
}
}
thread::spawn(move || start_getting_weather());
我是新手,目前正在学习 Rust,来自 Go。我如何实现像长并发轮询这样的东西?
// StartGettingWeather initialize weather getter and setter
func StartGettingWeather() {
// start looping
for i := range time.Tick(time.Second * time.Duration(delay)) {
_ = i
loopCounter++
fmt.Println(time.Now().Format(time.RFC850), " counter: ", loopCounter)
mainWeatherGetter()
}
}
我将把这个函数称为 go StartGettingWeather()
Rust 线程是 OS 线程,它们使用 OS 调度程序,因此您可以使用 thread::sleep_ms
:
use std::thread;
fn start_getting_weather() {
let mut loop_counter = 0;
loop {
loop_counter += 1;
println!("counter: {}", loop_counter);
main_weather_getter();
thread::sleep_ms(delay);
}
}
thread::spawn(move || start_getting_weather());