pub struct Pool { /* private fields */ }
Expand description
A process pool.
This works similar to spawn
but lets you retain a pool of processes. Since
procspawn is intended to isolate potentially crashing code the pool will
automatically restart broken processes.
Note that it’s not possible to intercept streams of processes spawned through the pool.
When the process pool is dropped all processes are killed.
This requires the pool
feature.
Implementations§
source§impl Pool
impl Pool
sourcepub fn new(size: usize) -> Result<Pool, SpawnError>
pub fn new(size: usize) -> Result<Pool, SpawnError>
Creates the default pool.
Examples found in repository?
examples/pool.rs (line 8)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
procspawn::init();
let pool = Pool::new(4).unwrap();
let mut handles = vec![];
for counter in 0..8 {
handles.push(procspawn::spawn!(in pool, (counter) || {
thread::sleep(Duration::from_millis(500));
counter
}));
}
for handle in handles {
match handle.join() {
Ok(val) => println!("got result: {}", val),
Err(err) => {
let panic = err.panic_info().expect("got a non panic error");
println!("process panicked with {}", panic.message());
println!("{:#?}", panic);
}
}
}
pool.shutdown();
}
sourcepub fn builder(size: usize) -> PoolBuilder
pub fn builder(size: usize) -> PoolBuilder
Creates a builder to customize pool creation.
sourcepub fn queued_count(&self) -> usize
pub fn queued_count(&self) -> usize
Returns the number of jobs waiting to executed in the pool.
sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Returns the number of currently active threads.
sourcepub fn spawn<A: Serialize + DeserializeOwned, R: Serialize + DeserializeOwned + Send + 'static>(
&self,
args: A,
func: fn(_: A) -> R
) -> JoinHandle<R>
pub fn spawn<A: Serialize + DeserializeOwned, R: Serialize + DeserializeOwned + Send + 'static>( &self, args: A, func: fn(_: A) -> R ) -> JoinHandle<R>
Spawns a closure into a process of the pool.
This works exactly like procspawn::spawn
but instead
of spawning a new process, it reuses a process from the pool.
sourcepub fn shutdown(&self)
pub fn shutdown(&self)
Joins and shuts down.
Examples found in repository?
examples/pool.rs (line 29)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
procspawn::init();
let pool = Pool::new(4).unwrap();
let mut handles = vec![];
for counter in 0..8 {
handles.push(procspawn::spawn!(in pool, (counter) || {
thread::sleep(Duration::from_millis(500));
counter
}));
}
for handle in handles {
match handle.join() {
Ok(val) => println!("got result: {}", val),
Err(err) => {
let panic = err.panic_info().expect("got a non panic error");
println!("process panicked with {}", panic.message());
println!("{:#?}", panic);
}
}
}
pool.shutdown();
}
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Pool
impl Send for Pool
impl Sync for Pool
impl Unpin for Pool
impl UnwindSafe for Pool
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more