Handle

Struct Handle 

Source
pub struct Handle { /* private fields */ }
Expand description

A handle to a child process created via Spawner::spawn() If input/output/error redirection were setup in the Spawner, you can use their related functions to access them.

Additionally, if there are other associated handles (Such as an auxiliary task to the one launched by the handle), you can delegate them as associates and allow the caller to manage their lifetimes. This allows you to only manage a single handle, with all its associates being cleanup when it does.

You should never construct a Handle yourself, it should always be returned through a Spawner.

Implementations§

Source§

impl Handle

Source

pub fn new( name: String, pid: Pid, mode: Mode, stdin: Option<OwnedFd>, stdout: Option<OwnedFd>, stderr: Option<OwnedFd>, associates: Vec<Handle>, ) -> Self

Construct a new Handle from a Child PID and pipes

Source

pub fn name(&self) -> &str

Get the name of the handle.

Source

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

Get the pid of the child.

Source

pub fn wait_timeout(self, timeout: Duration) -> Result<i32, Error>

Wait for the child to exit, with a timeout in case of no activity.

Note that this function uses a signal handler to ensure it does not hang the process, as well as efficiently wait the timeout. You cannot use this function in multi-threaded environments.

Source

pub fn wait_and(&mut self) -> Result<i32, Error>

Wait for the child to exit.

Note that this function uses a signal handler to ensure it does not hang the process. You cannot use this function in multi-threaded environments.

Source

pub fn wait(self) -> Result<i32, Error>

Consume the handle and return the exit code of the process.

Source

pub fn wait_blocking(&mut self) -> Result<i32, Error>

Wait for the child without signal handlers.

This function is a thread-safe version of wait, but means that signals will not be caught.

Source

pub fn alive(&mut self) -> Result<Option<Pid>, Error>

Check if the process is still alive, non-blocking.

Source

pub fn terminate(&mut self) -> Result<(), Error>

Terminate the process with a SIGTERM request, but do not consume the Handle.

Source

pub fn signal(&mut self, sig: Signal) -> Result<(), Error>

Send a signal to the child.

Source

pub fn signal_group(&mut self, sig: Signal) -> Result<(), Error>

Send the signal to the child, and all associated handles.

Source

pub fn detach(self) -> Option<Pid>

Detach the thread from manual cleanup. This function does nothing more than move the Pid of the child out of the Handle. When the Handle falls out of scope, it will not have a Pid to terminate, so the child process will linger.

Spawner sets Death Sig to SIGKILL, which means that when the parent dies, its children are sent SIGKILL. This means a detached thread should not become a Zombie Process, even if the Pid is dropped on program exit.

You therefore have three options on what to do with the return value of this function:

  1. If there was no child to detach, you’ll get a None, and do nothing.
  2. If you want to manage the child yourself (Or associate it with another Handle), capture the value.
  3. If you want to truly detach it, don’t capture the return value. It will run in the background, and will be killed if its still running at program exit.
Source

pub fn get_associate(&mut self, name: &str) -> Option<&mut Handle>

Returns a mutable reference to an associate within the Handle, if it exists. The associate is another Handle instance.

Source

pub fn error(&mut self) -> Result<&mut Stream, Error>

Return the Stream associated with the child’s standard error, if it exists. Note that pulling from the Stream consumes the contents–calling error_all will only return the contents from when you last pulled from the Stream.

Source

pub fn error_all(self) -> Result<String, Error>

Waits for the child to terminate, then returns its entire standard error.

Source

pub fn output(&mut self) -> Result<&mut Stream, Error>

Return the Stream associate with the child’s standard output, if it exists. Note that pulling from the Stream consumes the contents–calling output_all will only return the contents from when you last pulled from the Stream.

Source

pub fn output_all(self) -> Result<String, Error>

Waits for the child to terminate, then returns its entire standard output. If you need the exit code, use wait() first.

Source

pub fn close(&mut self) -> Result<(), Error>

Closes the Handle’s side of the standard input pipe, if it exists. This sends an EOF to the child.

Trait Implementations§

Source§

impl Drop for Handle

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Write for Handle

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl Freeze for Handle

§

impl !RefUnwindSafe for Handle

§

impl Send for Handle

§

impl Sync for Handle

§

impl Unpin for Handle

§

impl !UnwindSafe for Handle

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.