Crate tokio_signal

Source
Expand description

Asynchronous signal handling for Tokio

Note: This crate is deprecated in tokio 0.2.x and has been moved into tokio::signal behind the signal feature flag.

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

extern crate futures;
extern crate tokio;
extern crate tokio_signal;

use futures::{Future, Stream};

fn main() {
    // 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::ctrl_c().flatten_stream();

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

    tokio::runtime::current_thread::block_on_all(prog).unwrap();
}

Wait for SIGHUP on Unix

extern crate futures;
extern crate tokio;
extern crate tokio_signal;

use futures::{Future, Stream};
use tokio_signal::unix::{Signal, SIGHUP};

fn main() {
    // 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).flatten_stream();

    // Convert out stream into a future and block the program
    tokio::runtime::current_thread::block_on_all(stream.into_future()).ok().unwrap();
}

Modules§

  • Windows-specific types for signal handling.

Functions§

  • Creates a stream which receives “ctrl-c” notifications sent to a process.
  • Creates a stream which receives “ctrl-c” notifications sent to a process.

Type Aliases§

  • A future whose error is io::Error
  • A stream whose error is io::Error