#mio #epoll #kqueue #iocp #wepoll

polling

Portable interface to epoll, kqueue, event ports, and wepoll

21 releases (11 stable)

2.2.0 Nov 10, 2021
2.1.0 Jun 14, 2021
2.0.3 Mar 20, 2021
2.0.2 Oct 18, 2020
0.1.6 Aug 29, 2020

#10 in Asynchronous

Download history 97601/week @ 2021-08-11 128192/week @ 2021-08-18 103216/week @ 2021-08-25 107209/week @ 2021-09-01 122048/week @ 2021-09-08 114722/week @ 2021-09-15 100673/week @ 2021-09-22 108681/week @ 2021-09-29 130005/week @ 2021-10-06 132640/week @ 2021-10-13 109942/week @ 2021-10-20 114581/week @ 2021-10-27 116419/week @ 2021-11-03 117748/week @ 2021-11-10 115555/week @ 2021-11-17 98859/week @ 2021-11-24

471,501 downloads per month
Used in 1,544 crates (5 directly)

Apache-2.0 OR MIT

60KB
1K SLoC

polling

Build License Cargo Documentation

Portable interface to epoll, kqueue, event ports, and wepoll.

Supported platforms:

  • epoll: Linux, Android
  • kqueue: macOS, iOS, FreeBSD, NetBSD, OpenBSD, DragonFly BSD
  • event ports: illumos, Solaris
  • poll: VxWorks, Fuchsia, other Unix systems
  • wepoll: Windows

Polling is done in oneshot mode, which means interest in I/O events needs to be reset after an event is delivered if we're interested in the next event of the same kind.

Only one thread can be waiting for I/O events at a time.

Examples

use polling::{Event, Poller};
use std::net::TcpListener;

// Create a TCP listener.
let socket = TcpListener::bind("127.0.0.1:8000")?;
socket.set_nonblocking(true)?;
let key = 7; // Arbitrary key identifying the socket.

// Create a poller and register interest in readability on the socket.
let poller = Poller::new()?;
poller.add(&socket, Event::readable(key))?;

// The event loop.
let mut events = Vec::new();
loop {
    // Wait for at least one I/O event.
    events.clear();
    poller.wait(&mut events, None)?;

    for ev in &events {
        if ev.key == key {
            // Perform a non-blocking accept operation.
            socket.accept()?;
            // Set interest in the next readability event.
            poller.modify(&socket, Event::readable(key))?;
        }
    }
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~135KB