Created
August 14, 2021 21:47
-
-
Save likeabbas/695c51d8b794e35c6a96e6a9629856a1 to your computer and use it in GitHub Desktop.
Rust select! example to cancel from outside
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let (send, recv) = oneshot::channel::<()>(); | |
tokio::spawn(async { | |
tokio::select! { | |
_ = task_inner() => {}, | |
_ = recv => { | |
info!("finished, break the loop, call it a day"); | |
} | |
} | |
info!("finished in the walking task"); | |
}); | |
async fn task_inner() { | |
let mut interval = time::interval(Duration::from_secs(1)); | |
loop { | |
interval.tick().await; | |
info!("interval, do the work here"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Saving this as a gist for my own future use. Original creator author was here https://users.rust-lang.org/t/tokio-interval-and-cancel-from-outside/55108