I've wrote a small process manager in Rust that I've been interested in migrating to Nim due to the languages ease of use. Does anyone know if the std provides the ability to spawn new processes and monitor their current state?
Example:
loop {
for (agent, process) in &mut agents {
match process.try_wait() {
Ok(Some(status)) => {
println!("Exited with status {}", status);
*process = Command::new("python")
.arg("worker.py")
.arg(name.as_ref())
.spawn()
.expect("worker failed to start");
},
Ok(None) => println!("timeout, process is still alive: {}", agent),
Err(e) => println!("Error waiting: {}", e),
};
}
}
The problem with that is that osproc.waitForExit locks. Rust's try_wait does not lock:
https://doc.rust-lang.org/std/process/struct.Child.html#method.try_wait
This function will not block the calling thread and will only check to see if the child process has exited or not. If the child has exited then on Unix the process ID is reaped. This function is guaranteed to repeatedly return a successful exit status so long as the child has already exited.
Using asynctools + peekExitCode?
https://github.com/mratsim/constantine/blob/master/helpers/pararun.nim
I think the Chronos library also offers similar.
Indeed, chronos is the successor to asynctools and implements non-blocking process handling in general - here are some examples:
https://github.com/status-im/nim-chronos/blob/master/tests/testproc.nim#L18