#pool #tokio #future #multi-thread

yanked tokio-pool

TokioPool is a library designed to help you multithread your tokio applications by providing a pool of threads which you can distribute your load across

Uses old Rust 2015

0.1.0 Dec 28, 2016

#22 in #multi-thread

MIT license

5KB
64 lines

tokio-pool

Documentation

TokioPool is a library designed to help you multithread your tokio applications by providing a pool of threads which you can distribute your load across.


lib.rs:

TokioPool is a library designed to help you multithread your tokio applications by providing a pool of threads which you can distribute your load across.

Example

#
#
#
// Create a pool with 4 workers
let (pool, join) = TokioPool::new(4)
    .expect("Failed to create event loop");
// Wrap it in an Arc to share it with the listener worker
let pool = Arc::new(pool);
// We can listen on 8080
let addr: SocketAddr = "0.0.0.0:8080".parse().unwrap();

// Clone the pool reference for the listener worker
let pool_ref = pool.clone();
// Use the first pool worker to listen for connections
pool.next_worker().spawn(move |handle| {
    // Bind a TCP listener to our address
    let listener = TcpListener::bind(&addr, handle).unwrap();
    // Listen for incoming clients
    listener.incoming().for_each(move |(socket, addr)| {
        pool_ref.next_worker().spawn(move |handle| {
            // Do work with a client socket
        });

        Ok(())
    }).map_err(|_| ()) // You might want to log these errors or something
});

// You might call `join.join()` here, I don't in this example so that
// `cargo test` doesn't wait forever.

Dependencies

~6MB
~94K SLoC