wpool
A thread pool that limits the number of tasks executing concurrently, without restricting how many tasks can be queued. Submitting tasks is non-blocking, so you can enqueue any number of tasks without waiting.
This library is essentially a port of workerpool, an amazing Go library.
Examples
Worker Maximum
max_workers must be greater than 0! If max_workers == 0 we panic.
use WPool;
// At most 10 workers can run at once.
let max_workers = 10;
let pool = new;
// Submit as many functions as you'd like.
pool.submit;
pool.submit;
// Block until all workers are done working and
// the waiting queue has been processed.
pool.stop_wait;
Worker Maximum and Minimum
min_workers defines (up to) the minimum number of worker threads that should always stay alive, even when the pool is idle. If min_workers is greater than max_workers, we panic.
NOTE: We do not 'pre-spawn' workers! Meaning, if you set min_workers = 3 but your pool only ever creates 2 workers, then only 2 workers will ever exist (and should always be alive).
use WPool;
// At most 10 workers can run at once.
let max_workers = 10;
// At minimum up to 3 workers should always exist.
let min_workers = 3;
let pool = new_with_min;
// Submit as many functions as you'd like.
pool.submit;
pool.submit;
// Block until all workers are done working and
// the waiting queue has been processed.
pool.stop_wait;
Capturing
use WPool;
let wp = new;
let my_foo = Foo ;
// You can either clone before capturing:
let my_foo_clone = my_foo.clone;
wp.submit;
// Or allow the closure to consume (eg. move ownership):
wp.submit;
wp.stop_wait;
Submit as Fast as Possible
You just want to submit jobs as fast as possible without any blocking. Does not wait for any sort of confirmation.
Does not block under any circumstance by default.
use WPool;
let wp = new;
for i in 1..=100
Wait for Submission to Execute
use WPool;
let wp = new;
// Will block here until job is *executed*.
wp.submit_wait;
wp.stop_wait;
Wait for Submission to be Submitted
Wait until your submission is either given to a worker or queued. Does not wait for you submission to be executed, only submitted.
This can be useful in loops when you need to know that everything has been submitted. Meaning, you now know workers have for sure been spawned.
use WPool;
use thread;
use Duration;
let max_workers = 5;
let wp = new;
for i in 1..=max_workers
assert_eq!;
wp.stop_wait;
Get Results from Job
use WPool;
use thread;
use Duration;
use mpsc;
let max_workers = 2;
let wp = new;
let expected_result = 88;
let = ;
// Clone sender and pass into job.
let tx_clone = tx.clone;
wp.submit;
// Pause until we get our result. This is not necessary in this case, as our channel
// is acting as a pseudo pauser. If we were using an unbounded channel, we may want
// to use pause in order to wait for the result of any running task (like if we need
// to use the result elsewhere).
//
// wp.pause();
match rx.recv
wp.stop_wait;
View Tasks that Panicked
We collect panic info which can be accessed via <instance>.get_workers_panic_info().
use WPool;
let wp = new;
wp.submit;
// Wait for currently running jobs to finish.
wp.pause;
println!;
// [
// PanicInfo {
// thread_id: ThreadId(
// 9,
// ),
// payload: Some(
// "something went wrong!",
// ),
// file: Some(
// "src/file.rs",
// ),
// line: Some(
// 163,
// ),
// column: Some(
// 19,
// ),
// },
// ]
wp.stop_wait;