Struct 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)
4fn main() {
5    procspawn::init();
6    let mut handle = spawn((), |()| loop {});
7    handle.kill().unwrap();
8}
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)
4fn main() {
5    procspawn::init();
6
7    let mut builder = procspawn::Builder::new();
8    builder.stdout(Stdio::piped());
9
10    let mut handle = builder.spawn((1, 2), |(a, b)| {
11        println!("{:?} {:?}", a, b);
12    });
13
14    let mut s = String::new();
15    handle.stdout().unwrap().read_to_string(&mut s).unwrap();
16    assert_eq!(s, "1 2\n");
17}
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)
3fn main() {
4    procspawn::init();
5
6    let handle = spawn((), |()| std::env::args().collect::<Vec<_>>());
7
8    let args = handle.join().unwrap();
9
10    println!("args in subprocess: {:?}", args);
11}
More examples
Hide additional examples
examples/simple.rs (line 11)
3fn main() {
4    procspawn::init();
5
6    let handle = spawn((1, 2), |(a, b)| {
7        println!("in process: {:?} {:?}", a, b);
8        a + b
9    });
10
11    println!("result: {}", handle.join().unwrap());
12}
examples/macro.rs (line 13)
3fn main() {
4    procspawn::init();
5
6    let a = 42u32;
7    let b = 23u32;
8    let c = 1;
9    let handle = spawn!((a => new_name1, b, mut c) || -> Result<_, ()> {
10        c += 1;
11        Ok(new_name1 + b + c)
12    });
13    let value = handle.join().unwrap();
14
15    println!("{:?}", value);
16}
examples/custom-serialization.rs (line 80)
71fn main() {
72    procspawn::init();
73
74    let bytes = MyBytes::open("Cargo.toml").unwrap();
75
76    let bytes_two = procspawn::spawn!((bytes.clone() => bytes) || {
77        println!("length: {}", bytes.bytes.len());
78        bytes
79    })
80    .join()
81    .unwrap();
82
83    assert_eq!(bytes, bytes_two);
84}
examples/join.rs (line 9)
3fn main() {
4    procspawn::init();
5
6    let five = spawn(5, fibonacci);
7    let ten = spawn(10, fibonacci);
8    let thirty = spawn(30, fibonacci);
9    assert_eq!(five.join().unwrap(), 5);
10    assert_eq!(ten.join().unwrap(), 55);
11    assert_eq!(thirty.join().unwrap(), 832_040);
12    println!("Successfully calculated fibonacci values!");
13}
examples/panic.rs (line 10)
3fn main() {
4    procspawn::init();
5
6    let handle = spawn((), |()| {
7        panic!("Whatever!");
8    });
9
10    match handle.join() {
11        Ok(()) => unreachable!(),
12        Err(err) => {
13            let panic = err.panic_info().expect("got a non panic error");
14            println!("process panicked with {}", panic.message());
15            println!("{:#?}", panic);
16        }
17    }
18}
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)
5fn main() {
6    procspawn::init();
7
8    let mut handle = spawn((), |()| {
9        thread::sleep(Duration::from_secs(10));
10    });
11
12    println!("result: {:?}", handle.join_timeout(Duration::from_secs(1)));
13}

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> !Freeze for JoinHandle<T>

§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.