-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tokio::time::interval is not working #6544
Comments
After the completion of the first loop, subsequent loops are almost always expected to terminate the second arm first, so the tick print will not continue as you expected. |
You need to use an if precondition instead of placing the if inside the async block. That way, the branch will be disabled instead of triggering immediately. See the documentation on |
@Darksonn async fn waitfor(s: &mut bool) {
if *s {
*s = false;
// If comment out this line, the timer will work properly
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
}
}
#[test]
fn test() {
tokio::runtime::Runtime::new().unwrap().block_on(async {
tokio::spawn(async move {
let mut timer = tokio::time::interval(tokio::time::Duration::from_secs(1));
let mut s = true;
loop {
tokio::select! {
_= timer.tick() => {
println!("----tick");
}
_ = waitfor(&mut s) => {}
}
}
})
.await
.unwrap();
});
} |
You can also await |
Version
Platform
Description
Test code is as follows:
I expect to output the following content every second, but it doesn't work:
-----tick
-----tick
-----tick
-----tick
-----tick
-----tick
...
The text was updated successfully, but these errors were encountered: