I just started learning Rust. I have a websocket server using Tokio. Now I’m trying to run something in an interval in a task, but be able to cancel the interval from the outside. The below bit doesn’t work because recv-in-a-loop-error (I hope you can see the error yourself, on a phone so don’t have the error right now). Should I be doing things differently instead? Maybe just hold a flag somewhere to be able to signal the end of the interval? Thanks, I hope the beginner question is fine over here!
let (send, recv) = oneshot::channel::<()>();
tokio::spawn(async {
let mut interval = time::interval(Duration::from_secs(1));
tokio::pin!(interval);
loop {
tokio::select! {
_ = interval.tick() => {
info!("interval, do the work here");
}
_ = recv => {
info!("finished, break the loop, call it a day");
break;
}
}
}
info!("finished in the walking task");
});
// TODO: and use send to trigger the cancelation of the interval later on