workerpool
A worker threadpool used to execute a number of jobs atop stateful workers in parallel. It spawns a specified number of worker threads and replenishes the pool if any worker threads panic.
A single Worker runs in its own thread, to be implemented according to the trait:
Usage
[]
= "1.2"
To use crossbeam's
channels
instead of std::sync::mpsc,
enable the crossbeam feature:
[]
= { = "1.2", = ["crossbeam"] }
This crate provides Pool<W> where W: Worker. With a pool, there are four
primary functions of interest:
Pool::<MyWorker>::new(n_threads)creates a new pool for a particularWorker.pool.execute(inp)non-blocking executes the worker and ignores the return value.pool.execute_to(tx, inp)non-blocking executes the worker and sends return value to the given Sender.pool.join()blocking waits for all tasks (fromexecuteandexecute_to) to complete.
A worker is provided in workerpool::thunk, a stateless ThunkWorker<T>.
It executes on inputs of Thunk<T>, effectively argumentless functions that
are Sized + Send. These thunks are creates by wrapping functions which
return T with Thunk::of.
use Pool;
use ;
use channel;
For stateful workers, you have to implement Worker yourself.
Suppose there's a line-delimited process, such as cat or tr, which you'd
like running on many threads for use in a pool-like manner. You may create
and use a worker, with maintained state of the stdin/stdout for the process,
as follows:
use ;
use ;
use *;
use ;
use channel;
Similar libraries
License
This work is derivative of threadpool.
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
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.