import multiprocessing as mp
from time import sleep
import shortuuid
import os
import psutil
# 方法1 信号
import signal
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
class P(mp.Process):
def __init__(self, name):
super(P, self).__init__()
self.name = name
def run(self):
sleep(5)
if __name__ == '__main__':
ps = []
for _ in range(10):
name = str(shortuuid.uuid())
print(name)
ps.append(P(name))
ps[0].daemon = True
for p in ps:
p.start()
# 父进程id
pid = os.getpid()
p_g = psutil.Process(pid)
while True:
# print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>", flush=True)
# 所有子进程对象,包括执行中、sleeping、僵尸进程等
children = p_g.children()
if len(children) == 0:
break
print(children)
sleep(1)
# 方法2 waitpid,如果不存在子进程则抛异常
# try:
# os.waitpid(-1, os.WNOHANG)
# except Exception as e:
# break
print("finished")
waipid和signal



