Struct node_workers::WorkerPool
source · [−]pub struct WorkerPool { /* private fields */ }
Expand description
A pool of nodejs workers
Implementations
sourceimpl WorkerPool
impl WorkerPool
sourcepub fn setup(max_workers: usize) -> Self
pub fn setup(max_workers: usize) -> Self
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);
sourcepub fn with_debug(&mut self, debug: bool)
pub fn with_debug(&mut self, debug: bool)
Enable or disable logging
sourcepub fn run_worker<P: AsPayload>(
&mut self,
file_path: &str,
cmd: &str,
payload: P
) -> JoinHandle<Option<String>>
pub fn run_worker<P: AsPayload>(
&mut self,
file_path: &str,
cmd: &str,
payload: P
) -> JoinHandle<Option<String>>
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);
sourcepub fn run_task<T: DeserializeOwned, P: AsPayload>(
&mut self,
file_path: &str,
cmd: &str,
payloads: Vec<P>
) -> Result<Vec<Option<T>>>
pub fn run_task<T: DeserializeOwned, P: AsPayload>(
&mut self,
file_path: &str,
cmd: &str,
payloads: Vec<P>
) -> Result<Vec<Option<T>>>
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
impl RefUnwindSafe for WorkerPool
impl Send for WorkerPool
impl Sync for WorkerPool
impl Unpin for WorkerPool
impl UnwindSafe for WorkerPool
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more