处理队列中的中断系统调用

5 投票
2 回答
4856 浏览
提问于 2025-04-16 11:33

我们突然在队列操作中遇到了“中断的系统调用”这个问题,像这样:

Exception in thread Thread-2:
Traceback (most recent call last):
[ . . . ]
   result = self.pager.results.get(True, self.WAIT_SECONDS)
 File "/usr/lib/python2.5/site-packages/processing-0.52-py2.5-linux-x86_64.egg/processing/queue.py", line 128, in get
   if not self._poll(block and (deadline-time.time()) or 0.0):
IOError: [Errno 4] Interrupted system call

这是一个运行Fedora 10和Python 2.5的机器,最近进行了安全更新。在此之前,我们的软件运行了一年多没有出现问题,现在每天都崩溃。

我们是否应该捕获这个异常并重试队列操作呢?

我们没有设置任何信号处理程序,但这是一个Tkinter应用程序,也许它会设置一些。清除SIGINT处理程序是否安全,这样能解决问题吗?谢谢。

2 个回答

0

最后,这个问题在Python本身得到了修复,所以另一个解决办法就是更新到一个较新的Python版本:http://bugs.python.org/issue17097

7

根据在comp.lang.python上的这个讨论和Dan Stromberg的这个回复,我写了一个叫RetryQueue的东西,它可以直接替代Queue,并且能为我们完成任务:

from multiprocessing.queues import Queue
import errno

def retry_on_eintr(function, *args, **kw):
    while True:
        try:
            return function(*args, **kw)
        except IOError, e:            
            if e.errno == errno.EINTR:
                continue
            else:
                raise    

class RetryQueue(Queue):
    """Queue which will retry if interrupted with EINTR."""
    def get(self, block=True, timeout=None):
        return retry_on_eintr(Queue.get, self, block, timeout)

撰写回答