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
impl Handle
Sourcepub fn new(
name: String,
pid: Pid,
mode: Mode,
stdin: Option<OwnedFd>,
stdout: Option<OwnedFd>,
stderr: Option<OwnedFd>,
associates: Vec<Handle>,
) -> Self
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
Sourcepub fn wait_timeout(self, timeout: Duration) -> Result<i32, Error>
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.
Sourcepub fn wait_and(&mut self) -> Result<i32, Error>
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.
Sourcepub fn wait(self) -> Result<i32, Error>
pub fn wait(self) -> Result<i32, Error>
Consume the handle and return the exit code of the process.
Sourcepub fn wait_blocking(&mut self) -> Result<i32, Error>
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.
Sourcepub fn alive(&mut self) -> Result<Option<Pid>, Error>
pub fn alive(&mut self) -> Result<Option<Pid>, Error>
Check if the process is still alive, non-blocking.
Sourcepub fn terminate(&mut self) -> Result<(), Error>
pub fn terminate(&mut self) -> Result<(), Error>
Terminate the process with a SIGTERM request, but do not consume the Handle.
Sourcepub fn signal_group(&mut self, sig: Signal) -> Result<(), Error>
pub fn signal_group(&mut self, sig: Signal) -> Result<(), Error>
Send the signal to the child, and all associated handles.
Sourcepub fn detach(self) -> Option<Pid>
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:
- If there was no child to detach, you’ll get a None, and do nothing.
- If you want to manage the child yourself (Or associate it with another Handle), capture the value.
- 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.
Sourcepub fn get_associate(&mut self, name: &str) -> Option<&mut Handle>
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.
Sourcepub fn error(&mut self) -> Result<&mut Stream, Error>
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.
Sourcepub fn error_all(self) -> Result<String, Error>
pub fn error_all(self) -> Result<String, Error>
Waits for the child to terminate, then returns its entire standard error.
Sourcepub fn output(&mut self) -> Result<&mut Stream, Error>
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.
Sourcepub fn output_all(self) -> Result<String, Error>
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.
Trait Implementations§
Source§impl Write for Handle
impl Write for Handle
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)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> 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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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