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.
Pool with only worker maximum
// 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;
Pool with both worker maximum and worker minimum
min_workers defines (up to) the minimum number of worker threads that should always stay alive, even when the pool is idle.
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).
// 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;
Get results out of worker task
let max_workers = 2;
let wp = new;
let = ;
let tx_clone = tx.clone;
wp.submit;
// Pause until we get our result.
//
// (Note: pausing is not necessary in this example,
// as our channel can act 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;