$ cat test_a.py
from multiprocessing import Process, current_process
import logging
import os
import time
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)-15s - %(levelname)s - %(message)s'
)
def run():
logging.info('exit child process %s', current_process().pid)
os._exit(3)
p = Process(target=run)
p.start()
time.sleep(100)
$ python test_a.py & [1] 10091 $ 2017-07-20 21:28:14,792 - INFO - exit child process 10106 $ ps aux |grep 10106 mozillazg 10126 0.0 0.0 2434836 740 s006 R+ 0:00.00 grep 10106 mozillazg 10106 0.0 0.0 0 0 s006 Z 0:00.00 (Python)
import errno
from multiprocessing import Process, current_process
import logging
import os
import signal
import time
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)-15s - %(levelname)s - %(message)s'
)
def run():
exitcode = 3
logging.info('exit child process %s with exitcode %s',
current_process().pid, exitcode)
os._exit(exitcode)
def wait_child(signum, frame):
logging.info('receive SIGCHLD')
try:
while True:
# -1 表示任意子进程
# os.WNOHANG 表示如果没有可用的需要 wait 退出状态的子进程,立即返回不阻塞
cpid, status = os.waitpid(-1, os.WNOHANG)
if cpid == 0:
logging.info('no child process was immediately available')
break
exitcode = status >> 8
logging.info('child process %s exit with exitcode %s', cpid, exitcode)
except OSError as e:
if e.errno == errno.ECHILD:
logging.error('current process has no existing unwaited-for child processes.')
else:
raise
logging.info('handle SIGCHLD end')
signal.signal(signal.SIGCHLD, wait_child)
p = Process(target=run)
p.start()
while True:
time.sleep(100)
$ python test_b.py & [1] 10159 $ 2017-07-20 21:28:56,085 - INFO - exit child process 10174 with exitcode 3 2017-07-20 21:28:56,088 - INFO - receive SIGCHLD 2017-07-20 21:28:56,089 - INFO - child process 10174 exit with exitcode 3 2017-07-20 21:28:56,090 - ERROR - current process has no existing unwaited-for child processes. 2017-07-20 21:28:56,090 - INFO - handle SIGCHLD end $ ps aux |grep 10174 mozillazg 10194 0.0 0.0 2432788 556 s006 R+ 0:00.00 grep 10174
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有