Skip to main content

get_process_status

Function get_process_status 

Source
pub async fn get_process_status(
    child: &mut Child,
) -> Result<ProcessStatus, ProcessError>
Expand description

Asynchronously check process status without blocking.

Polls the process status in a non-blocking manner using tokio. Includes a small async sleep to prevent excessive CPU usage when called in a loop.

Note: This function may never return ProcessStatus::Done if the process is blocked waiting for stdin. Use stop_child and capture_exit_status to handle such cases.

§Arguments

  • child - Mutable reference to the child process

§Returns

Returns a Result<ProcessStatus, ProcessError> indicating the current process state.

§Examples

use ej_io::process::{spawn_process, get_process_status, ProcessStatus};

#[tokio::main]
async fn main() {
    let mut child = spawn_process("sleep", vec!["1".to_string()]).unwrap();

    loop {
        match get_process_status(&mut child).await.unwrap() {
            ProcessStatus::Done(exit_status) => {
                println!("Process finished with: {:?}", exit_status);
                break;
            }
            ProcessStatus::Running => {
                println!("Still running...");
            }
        }
    }
}