Ways torek are fine!
I found another way to handle a non-existent process;
we can use waitpid to redistribute a nonexistent process as necessary:
import os, subprocess, time def recycle_pid(): while True: try: pid, status, _ = os.wait3(os.WNOHANG) if pid == 0: break print("----- child %d terminated with status: %d" %(pid, status)) except OSError,e: break print("+++++ start pid:", subprocess.Popen("ls").pid) recycle_pid() print("+++++ start pid:", subprocess.Popen("ls").pid) recycle_pid() time.sleep(1) recycle_pid()
recycle_pid does not block, it can cause if necessary.
source share