Using subprocess concurrency
A subprocess is a command that is started from within another process. As a simple example of this, let's create a parent process with three children. process_a
will be the parent. Consider the following code snippet:
use std::process::Command; use std::env::current_exe; fn main() { let path = current_exe() .expect("could not find current executable"); let path = path.with_file_name("process_b"); let mut children = Vec::new(); for _ in 0..3 { children.push( Command::new(path.as_os_str()) .spawn() .expect("failed to execute process") ); } for mut c in children { c.wait() .expect("failed to wait on child"); } }
The child process, process_b
, runs a loop and prints its own process ID. This is shown as follows:
use std::{thread,time}; use std::process; fn main() { let t = time::Duration::from_millis(1000); loop { println!("process b #{}", process::id()...