Expand description
§Tokio Shutdown
Tiny crate that allows to wait for a stop signal across multiple threads. Helpful mostly in server applications that run indefinitely and need a signal for graceful shutdowns.
§Examples
This example installs the global shutdown handler and will print a message once a single is received. For demonstration purposes it creates other tasks that wait for shutdown as well.
use tokio_shutdown::Shutdown;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let shutdown = Shutdown::new().expect("shutdown creation works on first call");
// Pass a copy of the shutdown handler to another task.
// Clones of `Shutdown` are cheap.
let clone = shutdown.clone();
tokio::spawn(async move {
clone.handle().await;
println!("task 1 shutting down");
});
// Alternatively, pass a new handle to the new task.
// Both this and the above way work, choose whatever works best for your use case.
let handle = shutdown.handle();
tokio::spawn(async move {
handle.await;
println!("task 2 shutting down");
});
shutdown.handle().await;
println!("application shutting down");
}
Please have a look at the examples directory of this project for further usage instructions.
Structs§
- Already
Created Error - Error that occurs when the
Shutdown::new
function is called more than once in a process lifetime. - Shutdown
- The global shutdown handler for an application. It can be cloned cheaply wherever needed.