Struct procspawn::JoinHandle

source ·
pub struct JoinHandle<T> { /* private fields */ }
Expand description

An owned permission to join on a process (block on its termination).

The join handle can be used to join a process but also provides the ability to kill it.

Implementations§

source§

impl<T> JoinHandle<T>

source

pub fn pid(&self) -> Option<u32>

Returns the process ID if available.

The process ID is unavailable when pooled calls are not scheduled to processes.

source

pub fn kill(&mut self) -> Result<(), SpawnError>

Kill the child process.

If the join handle was created from a pool this call will do one of two things depending on the situation:

  • if the call was already picked up by the process, the process will be killed.
  • if the call was not yet scheduled to a process it will be cancelled.
Examples found in repository?
examples/kill.rs (line 7)
4
5
6
7
8
fn main() {
    procspawn::init();
    let mut handle = spawn((), |()| loop {});
    handle.kill().unwrap();
}
source

pub fn stdin(&mut self) -> Option<&mut ChildStdin>

Fetch the stdin handle if it has been captured

source

pub fn stdout(&mut self) -> Option<&mut ChildStdout>

Fetch the stdout handle if it has been captured

Examples found in repository?
examples/stdout.rs (line 15)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    procspawn::init();

    let mut builder = procspawn::Builder::new();
    builder.stdout(Stdio::piped());

    let mut handle = builder.spawn((1, 2), |(a, b)| {
        println!("{:?} {:?}", a, b);
    });

    let mut s = String::new();
    handle.stdout().unwrap().read_to_string(&mut s).unwrap();
    assert_eq!(s, "1 2\n");
}
source

pub fn stderr(&mut self) -> Option<&mut ChildStderr>

Fetch the stderr handle if it has been captured

source§

impl<T: Serialize + DeserializeOwned> JoinHandle<T>

source

pub fn join(self) -> Result<T, SpawnError>

Wait for the child process to return a result.

If the join handle was created from a pool the join is virtualized.

Examples found in repository?
examples/args.rs (line 8)
3
4
5
6
7
8
9
10
11
fn main() {
    procspawn::init();

    let handle = spawn((), |()| std::env::args().collect::<Vec<_>>());

    let args = handle.join().unwrap();

    println!("args in subprocess: {:?}", args);
}
More examples
Hide additional examples
examples/simple.rs (line 11)
3
4
5
6
7
8
9
10
11
12
fn main() {
    procspawn::init();

    let handle = spawn((1, 2), |(a, b)| {
        println!("in process: {:?} {:?}", a, b);
        a + b
    });

    println!("result: {}", handle.join().unwrap());
}
examples/macro.rs (line 13)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
    procspawn::init();

    let a = 42u32;
    let b = 23u32;
    let c = 1;
    let handle = spawn!((a => new_name1, b, mut c) || -> Result<_, ()> {
        c += 1;
        Ok(new_name1 + b + c)
    });
    let value = handle.join().unwrap();

    println!("{:?}", value);
}
examples/custom-serialization.rs (line 80)
71
72
73
74
75
76
77
78
79
80
81
82
83
84
fn main() {
    procspawn::init();

    let bytes = MyBytes::open("Cargo.toml").unwrap();

    let bytes_two = procspawn::spawn!((bytes.clone() => bytes) || {
        println!("length: {}", bytes.bytes.len());
        bytes
    })
    .join()
    .unwrap();

    assert_eq!(bytes, bytes_two);
}
examples/join.rs (line 9)
3
4
5
6
7
8
9
10
11
12
13
fn main() {
    procspawn::init();

    let five = spawn(5, fibonacci);
    let ten = spawn(10, fibonacci);
    let thirty = spawn(30, fibonacci);
    assert_eq!(five.join().unwrap(), 5);
    assert_eq!(ten.join().unwrap(), 55);
    assert_eq!(thirty.join().unwrap(), 832_040);
    println!("Successfully calculated fibonacci values!");
}
examples/panic.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    procspawn::init();

    let handle = spawn((), |()| {
        panic!("Whatever!");
    });

    match handle.join() {
        Ok(()) => unreachable!(),
        Err(err) => {
            let panic = err.panic_info().expect("got a non panic error");
            println!("process panicked with {}", panic.message());
            println!("{:#?}", panic);
        }
    }
}
source

pub fn join_timeout(&mut self, timeout: Duration) -> Result<T, SpawnError>

Like join but with a timeout.

Can be called multiple times. If anything other than a timeout error is returned, the handle becomes unusuable, and subsequent calls to either join or join_timeout will return an error.

Examples found in repository?
examples/timeout.rs (line 12)
5
6
7
8
9
10
11
12
13
fn main() {
    procspawn::init();

    let mut handle = spawn((), |()| {
        thread::sleep(Duration::from_secs(10));
    });

    println!("result: {:?}", handle.join_timeout(Duration::from_secs(1)));
}

Trait Implementations§

source§

impl<T> Debug for JoinHandle<T>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for JoinHandle<T>

§

impl<T> Send for JoinHandle<T>where T: Send,

§

impl<T> !Sync for JoinHandle<T>

§

impl<T> Unpin for JoinHandle<T>where T: Unpin,

§

impl<T> !UnwindSafe for JoinHandle<T>

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