Gunicorn + 子进程引发异常 [Errno 10]
我遇到了一个奇怪的异常,搞不清楚怎么解决... 有人能告诉我哪里出错了吗,或者给我一些新的设计思路吗?我正在运行一个Gunicorn/Flask的应用。在配置文件中,我指定了一些在on_starting
钩子中要执行的工作[1]。在这个钩子的代码里,我写了一些代码(没什么特别的):
# Called before the server is started
my_thread = package.MyThread()
my_thread.start()
package.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-hooks
[2] - Popen.communicate() 抛出 OSError: "[Errno 10] No child processes"
1 个回答
3
这个错误跟处理信号 SIGCHLD
有关。
gunicorn
的管理者会拦截 SIGCHLD
,这就导致 subprocess.Popen
出问题了。subprocess.Popen
这个模块需要 SIGCHLD
不被拦截(至少在 Python 2.6 及之前的版本是这样的)。
根据 bugs.python.org 的说法,这个问题在 Python 2.7 中已经修复了。