什么是正确的方法处理(在python)IOError:[Errno 4]中断的系统调用,由多处理引发.Queue.get [英] What is the proper way to handle (in python) IOError: [Errno 4] Interrupted system call, raised by multiprocessing.Queue.get

查看:180
本文介绍了什么是正确的方法处理(在python)IOError:[Errno 4]中断的系统调用,由多处理引发.Queue.get的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用multiprocessing.Queue.get时,我有时会因EINTR而获得异常。

When I use multiprocessing.Queue.get I sometimes get an exception due to EINTR.

我绝对知道有时这种情况发生没有好的原因(我打开另一个在这种情况下,我想继续工作并重试该操作。

I know definitely that sometimes this happens for no good reason (I open another pane in a tmux buffr), and in such a case I would want to continue working and retry the operation.

我可以想象,在其他一些情况下,错误将是因为一个很好的理由,我应该停止运行或修复一些错误。

I can imagine that in some other cases The error would be due to a good reason and I should stop running or fix some error.

我如何区分这两个?

提前感谢

推荐答案

EINTR 错误可以从许多系统调用当应用程序在等待其他输入时收到信号。通常,这些信号可能是相当良好的,已经由Python处理,但底层的系统调用仍然被中断。当执行C / C ++编码时,这就是为什么你不能完全依赖诸如 sleep()这样的功能的一个原因。 Python库有时在内部处理这个错误代码,但显然在这种情况下它们不是。

The EINTR error can be returned from many system calls when the application receives a signal while waiting for other input. Typically these signals can be quite benign and already handled by Python, but the underlying system call still ends up being interrupted. When doing C/C++ coding this is one reason why you can't entirely rely on functions like sleep(). The Python libraries sometimes handle this error code internally, but obviously in this case they're not.

您可能有兴趣阅读这个线程讨论了这个问题。

You might be interested to read this thread which discusses this problem.

对于 EINTR 的一般方法是简单地处理错误并重试该操作 - 这应该是一个安全的事情,与 get()方法在队列中。可以使用这样的东西,将队列作为参数传递,并替换在队列中使用 get()方法:

The general approach to EINTR is to simply handle the error and retry the operation again - this should be a safe thing to do with the get() method on the queue. Something like this could be used, passing the queue as a parameter and replacing the use of the get() method on the queue:

import errno

def my_queue_get(queue, block=True, timeout=None):
    while True:
        try:
            return queue.get(block, timeout)
        except IOError, e:
            if e.errno != errno.EINTR:
                raise

# Now replace instances of queue.get() with my_queue_get(queue), with other
# parameters passed as usual.

通常你不需要担心 EINTR 在Python程序中,除非你知道你正在等待一个特定的信号(例如 SIGHUP ),并且你已经安装了一个设置标志的信号处理程序,并依赖于代码的主体拿起国旗。在这种情况下,如果您收到 EINTR

Typically you shouldn't need to worry about EINTR in a Python program unless you know you're waiting for a particular signal (for example SIGHUP) and you've installed a signal handler which sets a flag and relies on the main body of the code to pick up the flag. In this case, you might need to break out of your loop and check the signal flag if you receive EINTR.

但是,如果你没有使用任何信号处理,那么你应该能够忽略 EINTR 并重复你的操作 - 如果Python本身需要用信号做某事应该已经在信号处理程序中处理了。

However, if you're not using any signal handling then you should be able to just ignore EINTR and repeat your operation - if Python itself needs to do something with the signal it should have already dealt with it in the signal handler.

这篇关于什么是正确的方法处理(在python)IOError:[Errno 4]中断的系统调用,由多处理引发.Queue.get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆