Python的多重退出错误(Python Multiprocessing exit error)

2019-07-31 13:06发布

我看到这个的时候我按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')

任何知道这个错误是什么? 我只得到它,当我有一个特定线程的多个实例。

Answer 1:

这与交互OS系统调用,信号和它是如何的多模块处理。 我真的不知道,如果它是一个错误或功能,但它在一定程度上棘手的领土,因为它是在那里蟒符合操作系统。

问题是,多被阻塞,waitpid函数,直到它的等待已经终止了孩子。 不过,既然你已经安装的信号处理程序SIGINT和程序得到这个信号,它中断系统调用执行信号处理程序,并waitpid函数退出,表明它是由一个信号中断。 该方法蟒蛇处理这种情况是例外。

作为一种变通方法,您可以封装在while循环有问题的部分(S)和try / 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()


Answer 2:

我看到这一点,但它走的时候我推翻信号处理我自己的。 使用reactor.run(installSignalHandlers = FALSE),并定义SIGINT,SIGTERM,等自己的函数



文章来源: Python Multiprocessing exit error