import os from multiprocessing import Process def run_child() -> None: print("Child: I am the child process") print(f"Child: Child's PID: {os.getpid()}") print(f"Child: Parent's PID: {os.getppid()}") def start_parent(num_children: int) -> None: print("Parent: I am the parent process") print(f"Parent : Parent's PID: {os.getpid()}") for i in range(num_children): print(f"Starting Process {i}") Process(target=run_child).start() if __name__ == "__main__": num_children = 3 start_parent(num_children)
$ python3 child_processes.py
Parent: I am the parent process
Parent : Parent’s PID: 576837
Starting Process 0
Starting Process 1
Starting Process 2
Child: I am the child process
Child: Child’s PID: 576838
Child: Parent’s PID: 576837
Child: I am the child process
Child: Child’s PID: 576840
Child: Parent’s PID: 576837
Child: I am the child process
Child: Child’s PID: 576839
Child: Parent’s PID: 576837
### Rustで書きたい
use std::process; use std::process::Command; fn main() { println!("My id is {}", process::id()); let mut command = Command::new("ls"); if let Ok(child) = command.spawn() { println!("My id is {}", child.id()); } }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/parallel`
My id is 577522
My id is 577561
Cargo.lock Cargo.toml python src target
Command::newはprocessってよりコマンドのprocessなのでちょっと意図しているものと違うな