How to call an external program without blocking (std::process::Command)

Hi all,
I'm trying to trigger an external program without blocking my program. The program I am calling will take some time to complete so I am trying to trigger it as a child process and then in my program I will poll for the result.

In this example the println!("Process spawned"); is not output:

if let Ok(mut child) = std::process::Command::new("external_program").spawn() {
    let timer = Instant::now();
    let mut result = false;
    println!("Process spawned");

    while timer.elapsed() < Duration::from_secs(5) {
        std::thread::sleep(Duration::from_secs(1)));

        if let Ok(exit_status) = child.try_wait() {
            if let Some(_status) = exit_status {
			    println!("Program completed");
                result = true;
                break;
            }
        } else {
            break;
        }
    }
}

Looks like the problem is somewhere else, since this code (with external_program being sleep 3) works well on the playground.

Ok, thanks for the feedback.

I ran into a similar issue, any solutions?

Apologies for the slow reply, I must have missed your response.

I was running the program in the debugger on a Linux OS via a remote GDB server. I found that the process was in the 't' state and the debugger was not showing me anything in the UI, so I was just unaware that anything had occurred. I didn't find a cause of why this started occurring for me, only that I could identify what was going on.

PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a
process:
 D uninterruptible sleep (usually IO)
I Idle kernel thread
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped by job control signal
t stopped by debugger during the tracing
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.