gpt4 book ai didi

Python多处理退出错误

转载 作者:太空狗 更新时间:2023-10-30 00:04:22 25 4
gpt4 key购买 nike

当我按 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')

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

最佳答案

这与交互 OS 系统调用、信号以及它在多处理模块中的处理方式有关。我不太确定它是错误还是功能,但它处于有点棘手的领域,因为它是 python 与操作系统的交汇点。

问题是多处理在 waitpid 上阻塞,直到它正在等待的子进程终止。然而,由于您已经为 SIGINT 安装了一个信号处理程序并且您的程序获得了这个信号,它会中断系统调用以执行您的信号处理程序,并且 waitpid 退出表明它被信号中断了。 python 处理这种情况的方式是通过异常。

作为一种解决方法,您可以将有问题的部分包含在 while 循环和 try/catch block 中,就像这样,或者在您等待线程完成的地方,或者子类 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()

关于Python多处理退出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1238349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com