Skip to content

Instantly share code, notes, and snippets.

@likeabbas
Created August 14, 2021 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save likeabbas/695c51d8b794e35c6a96e6a9629856a1 to your computer and use it in GitHub Desktop.
Save likeabbas/695c51d8b794e35c6a96e6a9629856a1 to your computer and use it in GitHub Desktop.
Rust select! example to cancel from outside
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");
}
}
@likeabbas
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment