pub struct WorkerPool { /* private fields */ }
Expand description

A pool of nodejs workers

Implementations

Create a new workers pool with the maximum numbers of workers that can be spawned for the duration of the program

use node_workers::{WorkerPool};

let nbr_max_workers = 4;
let mut pool = WorkerPool::setup(nbr_max_workers);

Enable or disable logging

Run a single worker in a thread. This method returns the created thread, not the result of the worker. Use this if you need more control on the pool.

use node_workers::{WorkerPool};

let mut pool = WorkerPool::setup(2);
for n in 1..=4 {
  pool.run_worker("examples/worker", "fib", n * 10);
}
println!("not blocking");

The returned thread optionally holds the serialized result from the worker. This can be deserialized using serde_json in order to get a proper result.

use node_workers::{WorkerPool};

let mut pool = WorkerPool::setup(2);
let handle = pool.run_worker("examples/worker", "fib2", 40u32);
let result = handle
  .join()
  .unwrap()
  .map(|x| serde_json::from_str::<u32>(x.as_str()).unwrap())
  .unwrap();
println!("run_worker result: {}", result);

Dispatch a task between available workers with a set of payloads. The length of the payloads defines how many workers are mobilised. But this also depends on the maximum number of allowed workers. As soon as a worker is free, it’ll be assigned right away a new task untill all payloads have been sent. Contrarly to run_worker, this method is blocking and directly return the result from all workers.

use node_workers::{WorkerPool};
let mut pool = WorkerPool::setup(2);
pool.with_debug(true);
let payloads = vec![10, 20, 30, 40];
let result = pool.run_task::<u64, _>("examples/worker", "fib2", payloads).unwrap();
println!("result: {:?}", result);

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.