python-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() throws OSError: “[Errno 10] No child processes”

解决方法:

事实证明该错误与SIGCHLD的信号处理有关.

金枪鱼仲裁器拦截SIGCHLD,这会破坏subprocess.Popen. subprocess.Popen模块要求不拦截SIGCHLD(至少对于Python 2.6和更早版本是这样).

根据bugs.python.org,此错误已在Python 2.7中修复.

上一篇:Python OpenVPN脚本


下一篇:reduce内置高阶函数求和