Python多处理退出错误

时间:2009-08-06 11:37:58

标签: python exception multiprocessing

当我按Ctrl-C退出我的应用程序

时,我看到了这一点
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/usr/lib/python2.6/multiprocessing/util.py", line 269, in _exit_function
    p.join()
  File "/usr/lib/python2.6/multiprocessing/process.py", line 119, in join
    res = self._popen.wait(timeout)
  File "/usr/lib/python2.6/multiprocessing/forking.py", line 117, in wait
    return self.poll(0)
  File "/usr/lib/python2.6/multiprocessing/forking.py", line 106, in poll
    pid, sts = os.waitpid(self.pid, flag)
OSError: [Errno 4] Interrupted system call
Error in sys.exitfunc:
Traceback (most recent call last):
  File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/usr/lib/python2.6/multiprocessing/util.py", line 269, in _exit_function
    p.join()
  File "/usr/lib/python2.6/multiprocessing/process.py", line 119, in join
    res = self._popen.wait(timeout)
  File "/usr/lib/python2.6/multiprocessing/forking.py", line 117, in wait
    return self.poll(0)
  File "/usr/lib/python2.6/multiprocessing/forking.py", line 106, in poll
    pid, sts = os.waitpid(self.pid, flag)
OSError: [Errno 4] Interrupted system call

我在自己的东西上使用扭曲,

我使用以下代码

注册了信号Ctrl-C
    def sigHandler(self, arg1, arg2):
        if not self.backuped:
            self.stopAll()
        else:
            out('central', 'backuped ALREADY, now FORCE exiting')
            exit()


    def stopAll(self):
        self.parserM.shutdown()
        for each in self.crawlM:
            each.shutdown()
        self.backup()
        reactor.stop()

当他们发出其他人关闭的信号时,它会试图告诉他们关机 通过

exit = multiprocessing.Event()
def shutdown(self):
    self.exit.set()

我的所有流程都采用某种形式,

def run(self):
    while not self.exit.is_set():
        do something
    out('crawler', 'crawler exited sucessfully')

知道这个错误是什么吗?当我有一个特定线程的多个实例时,我才会得到它。

2 个答案:

答案 0 :(得分:5)

这与交互操作系统调用,信号及其在多处理模块中的处理方式有关。我不确定它是否是一个bug或一个功能,但它在某些方面很棘手,因为它是python遇到操作系统的地方。

问题是多处理在waitpid上阻塞,直到它等待的孩子终止。但是,由于您已经为SIGINT安装了信号处理程序,并且您的程序获得了此信号,它会中断系统调用以执行您的信号处理程序,并且waitpid退出表示它已被信号中断。 python处理这种情况的方式是例外。

作为一种变通方法,您可以将有问题的部分包含在while循环中,并尝试使用这样的/ catch块,在等待线程完成的位置,或子类multiprocessing.Popen:

import errno
from multiprocessing import Process

p = Process( target=func, args=stuff )
p.start()
notintr = False
while not notintr:
  try:
    p.join() # "Offending code"
    notintr = True
  except OSError, ose:
    if ose.errno != errno.EINTR:
      raise ose

对于使用multiprocessing.forking.Popen进行捣乱你必须做这样的事情:

import errno
from multiprocessing import Process
from multiprocessing.forking import Popen
import os

# see /path/to/python/libs/multiprocessing/forking.py
class MyPopen(Popen):
  def poll(self, flag=os.WNOHANG): # from forking.py
    if self.returncode is None: # from forking.py
      notintr = False
      while not notintr:
        try:
          pid, sts = os.waitpid(self.pid, flag) # from forking.py
          notintr = True
        except OSError, ose:
          if ose.errno != errno.EINTR:
            raise ose
      # Rest of Popen.poll from forking.py goes here

p = Process( target=func args=stuff )
p._Popen = p
p.start()
p.join()

答案 1 :(得分:0)

我看到了这一点,但是当我用自己的信号处理器覆盖信号处理程序时,它就消失了。使用reactor.run(installSignalHandlers = False)并为SIGINT,SIGTERM等定义自己的函数。