在 python 守护进程(使用python-daemon )中使用多处理模块时出现以下错误:
回溯(最近一次通话最后): _run_exitfuncs 中的文件“/usr/local/lib/python2.6/atexit.py”,第 24 行 函数(*目标,**卡格斯) _exit_function 中的文件“/usr/local/lib/python2.6/multiprocessing/util.py”,第 262 行 对于 active_children() 中的 p: 文件“/usr/local/lib/python2.6/multiprocessing/process.py”,第 43 行,在 active_children _清理() _cleanup 中的文件“/usr/local/lib/python2.6/multiprocessing/process.py”,第 53 行 如果 p._popen.poll() 不是无: 轮询中的文件“/usr/local/lib/python2.6/multiprocessing/forking.py”,第 106 行 pid, sts = os.waitpid(self.pid, flag) OSError: [Errno 10] 没有子进程
守护进程(父进程)产生许多进程(子进程),然后定期轮询这些进程以查看它们是否已完成。如果父进程检测到其中一个进程已完成,则它会尝试重新启动该进程。正是在这一点上引发了上述异常。似乎一旦其中一个进程完成,任何涉及多处理模块的操作都会生成此异常。如果我在非守护进程 python 脚本中运行相同的代码,它执行时不会出现任何错误。
编辑:
示例脚本
from daemon import runner
class DaemonApp(object):
def __init__(self, pidfile_path, run):
self.pidfile_path = pidfile_path
self.run = run
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
def run():
import multiprocessing as processing
import time
import os
import sys
import signal
def func():
print 'pid: ', os.getpid()
for i in range(5):
print i
time.sleep(1)
process = processing.Process(target=func)
process.start()
while True:
print 'checking process'
if not process.is_alive():
print 'process dead'
process = processing.Process(target=func)
process.start()
time.sleep(1)
# uncomment to run as daemon
app = DaemonApp('/root/bugtest.pid', run)
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
#uncomment to run as regular script
#run()