You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because the official documentation is misleading, tokio-rs/tokio#4231, resulting in the current graceful shutdown mechanism is actually incorrect, need to fix and explore how to simplify.
The text was updated successfully, but these errors were encountered:
I'm going to create a tokio::sync::mpsc channel, and let all tokio tasks get the ownership of a Sender, and wait the Receiver in fn main(), when all tokio tasks finished, Sender will be dropped.
And use tokio_util::sync::CancellationToken to notify all tokio tasks to exit.
use tokio::sync::mpsc::{channel,Sender};use tokio::time::{sleep,Duration};#[tokio::main]asyncfnmain(){let(send,mut recv) = channel(1);for i in0..10{
tokio::spawn(some_operation(i, send.clone()));}// Wait for the tasks to finish.//// We drop our sender first because the recv() call otherwise// sleeps forever.drop(send);// When every sender has gone out of scope, the recv call// will return with an error. We ignore the error.let _ = recv.recv().await;}asyncfnsome_operation(i:u64,_sender:Sender<()>){sleep(Duration::from_millis(100* i)).await;println!("Task {} shutting down.", i);// sender goes out of scope ...}
Ref: "Waiting for things to finish shutting down" Section on tokio shutdown
Because the official documentation is misleading, tokio-rs/tokio#4231, resulting in the current graceful shutdown mechanism is actually incorrect, need to fix and explore how to simplify.
The text was updated successfully, but these errors were encountered: