Struct procspawn::Pool

source ·
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

source

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();
}
source

pub fn builder(size: usize) -> PoolBuilder

Creates a builder to customize pool creation.

source

pub fn size(&self) -> usize

Returns the size of the pool.

source

pub fn queued_count(&self) -> usize

Returns the number of jobs waiting to executed in the pool.

source

pub fn active_count(&self) -> usize

Returns the number of currently active threads.

source

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.

source

pub fn join(&self)

Joins the process pool.

source

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();
}
source

pub fn kill(&self)

Hard kills all processes in the pool.

After calling this the pool cannot be used any more.

Trait Implementations§

source§

impl Debug for Pool

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Pool

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V