我偶然发现了一个无法解决的奇怪异常。。。有人能提出什么是错误的或新的设计吗?我正在运行Gunicorn/Flask应用程序。在配置文件中,我指定了一些使用on_starting
钩子[1]要做的工作。在那个钩子代码中,我有一些这样的代码(没有什么花哨的):
# Called before the server is started
my_thread = package.MyThread()
my_thread.start()
包裹。MyThread类如下所示。ls
命令并不重要,它可以是任何命令。
class MyThread(threading.Thread):
"""
Run a command every 60 seconds.
"""
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()
def run(self):
while not self.event.is_set():
ptest = subprocess.Popen(["ls"], stdout=subprocess.PIPE)
ptest.communicate()
self.event.wait(60)
def stop(self):
self.event.set()
在启动服务器时,我总是看到这个例外:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "__init__.py", line 195, in run
ptest.communicate()
File "/usr/lib64/python2.6/subprocess.py", line 721, in communicate
self.wait()
File "/usr/lib64/python2.6/subprocess.py", line 1288, in wait
pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
File "/usr/lib64/python2.6/subprocess.py", line 462, in _eintr_retry_call
return func(*args)
OSError: [Errno 10] No child processes
有人能告诉我这里发生了什么吗?我还没有尝试实现[2]中的更改,它们看起来很粗糙。
[1] -http://gunicorn.org/configure.html#server-挂钩
[2] -Popen.communicate()抛出OSError:"[Erno 10]没有子进程";
该错误与SIGCHLD
的信号处理有关。
gunicorn
仲裁器截获SIGCHLD
,从而中断subprocess.Popen
。subprocess.Popen
模块要求不截获SIGCHLD
(至少,对于Python2.6及更早版本是这样)。
根据bugs.python.org,这个错误已经在python 2.7中修复。