每当继承 mulitprocessing.Process
类并将 SIGCHLD
信号绑定(bind)到其中时,python 解释器会引发以下错误:
Exception AttributeError: "'MyClass' object has no attribute '_popen'" in <Finalize object, dead> ignored
但是我的类(class)有预期的行为。
这是信号处理的样子:
import os
import signal
import multiprocessing
class Actor(multiprocessing.Process):
def __init__(self, *args, **kwargs):
self.bind_signals_handlers()
...
super(Actor, self).__init__(*args, **kwargs)
def on_children_exit(self):
"""Signal handler to be called on SIGCHLD.
This handler will be called on every SIGCHLD signal received
by the actor. SIGCHLD is raised on process interuptions and on
process death, so you can override this handler to set up
the behavior you'd like.
As a default this handler will check if a child process
as exited, and that's it.
"""
# Make sure we do the waitpid over the parent process
# and not from any children.
# see http://stackoverflow.com/questions/5783183/python-multiprocessing-issue-misunderstanding
# see http://bugs.python.org/issue9535
if os.getpid() == self.pid:
pid, status = os.waitpid(-1, os.WNOHANG)
if pid:
print "Actor's child with pid {} exited".format(pid)
def bind_signals_handlers(self):
def sig_child_handler(signum, frame):
self.on_children_exit()
def sig_shutdown_handler(signum, frame):
self.stop()
signal.signal(signal.SIGTERM, sig_shutdown_handler)
signal.signal(signal.SIGINT, sig_shutdown_handler)
signal.signal(signal.SIGCHLD, sig_child_handler)
是否知道导致警告的原因以及如何解决?
非常感谢!
最佳答案
我终于通过捕获 AttributeError
并检查回溯找到了解决方案。在 multiprocessing.Process
实例启动之前访问 pid
属性会引发 AttributeError
。
我还没有找到任何关于它的文档。
关于python - 在 Python 中绑定(bind)信号处理程序时,<Finalize object, dead> 中没有属性 '_popen'“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20841082/