#signal #tokio #future #ctrl-c #stream #notifications

tokio-signal

An implementation of an asynchronous Unix signal handling backed futures

16 releases

0.3.0-alpha.1 Aug 8, 2019
0.2.9 Feb 5, 2020
0.2.8 Mar 22, 2019
0.2.7 Nov 22, 2018
0.1.0 Sep 10, 2016

#19 in #ctrl-c

Download history 4273/week @ 2024-07-20 4004/week @ 2024-07-27 3265/week @ 2024-08-03 5398/week @ 2024-08-10 3958/week @ 2024-08-17 4666/week @ 2024-08-24 4234/week @ 2024-08-31 4176/week @ 2024-09-07 4198/week @ 2024-09-14 3308/week @ 2024-09-21 3190/week @ 2024-09-28 1693/week @ 2024-10-05 2356/week @ 2024-10-12 2314/week @ 2024-10-19 2833/week @ 2024-10-26 2015/week @ 2024-11-02

9,786 downloads per month
Used in 152 crates (22 directly)

MIT license

585KB
10K SLoC

tokio-signal

Unix signal handling for Tokio.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Tokio by you, shall be licensed as MIT, without any additional terms or conditions.


lib.rs:

Asynchronous signal handling for Tokio

This crate implements asynchronous signal handling for Tokio, an asynchronous I/O framework in Rust. The primary type exported from this crate, unix::Signal, allows listening for arbitrary signals on Unix platforms, receiving them in an asynchronous fashion.

Note that signal handling is in general a very tricky topic and should be used with great care. This crate attempts to implement 'best practice' for signal handling, but it should be evaluated for your own applications' needs to see if it's suitable.

The are some fundamental limitations of this crate documented on the Signal structure as well.

Examples

Print out all ctrl-C notifications received

#![feature(async_await)]

use futures_util::future;
use futures_util::stream::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create an infinite stream of "Ctrl+C" notifications. Each item received
    // on this stream may represent multiple ctrl-c signals.
    let ctrl_c = tokio_signal::CtrlC::new()?;

    // Process each ctrl-c as it comes in
    let prog = ctrl_c.for_each(|_| {
        println!("ctrl-c received!");
        future::ready(())
    });

    prog.await;

    Ok(())
}

Wait for SIGHUP on Unix

#![feature(async_await)]

use futures_util::future;
use futures_util::stream::StreamExt;
use tokio_signal::unix::{Signal, SIGHUP};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create an infinite stream of "Ctrl+C" notifications. Each item received
    // on this stream may represent multiple ctrl-c signals.
    let ctrl_c = tokio_signal::CtrlC::new()?;

    // Process each ctrl-c as it comes in
    let prog = ctrl_c.for_each(|_| {
        println!("ctrl-c received!");
        future::ready(())
    });

    prog.await;

    // Like the previous example, this is an infinite stream of signals
    // being received, and signals may be coalesced while pending.
    let stream = Signal::new(SIGHUP)?;

    // Convert out stream into a future and block the program
    let (signal, _signal) = stream.into_future().await;
    println!("got signal {:?}", signal);
    Ok(())
}

Dependencies

~3MB
~50K SLoC