2015-04-16 32 views
1

我是新来的,现在学习Rust,来自Go。我如何实现诸如长时间并发轮询之类的东西?什么是Go的范围time.Tick?

// 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()

+0

曾经有一个计时器在标准,但现在它已被弃用。你可以看看这个箱子的更换:https://github.com/PeterReid/timer。 periodic_ms应该或多或少地像时间一样工作。挑战 –

回答

2

锈线程操作系统线程,他们使用的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()); 
+0

嗨,谢谢你!,它运行良好。我想不应该用这个,再次感谢你。 – Hokutosei