由于在打印调用期间引起输入/输出错误,我已经继承了一些周期性(随机)失败的代码.我正在尝试确定提出异常的原因(或至少更好地了解它)以及如何正确处理它.
当执行以下Python行(在2.6.6解释器中,在CentOS 5.5上运行):
print >> sys.stderr, 'Unable to do something: %s' % command
异常被提出(traceback省略):
IOError: [Errno 5] Input/output error
对于上下文,这通常是当时更大的功能尝试做的事情:
from subprocess import Popen, PIPE
import sys
def run_commands(commands):
for command in commands:
try:
out, err = Popen(command, shell=True, stdout=PIPE, stderr=PIPE).communicate()
print >> sys.stdout, out
if err:
raise Exception('ERROR -- an error occurred when executing this command: %s --- err: %s' % (command, err))
except:
print >> sys.stderr, 'Unable to do something: %s' % command
run_commands(["ls", "echo foo"])
>>语法对我来说不是特别熟悉,这不是我经常使用的东西,我明白,这可能是写给stderr的least preferred way.但是我不相信替代方案会解决潜在的问题.
从我读到的文档中,IOError 5经常被滥用,并且有些松散地定义,使用不同的操作系统来覆盖不同的问题.在我的情况下我可以看到的最好的是,python进程不再附加到终端/ pty.
尽可能地告诉我什么是断开stdout / stderr流的进程 – 终端仍然是开放的,例如,所有的东西都显得很好.这可能是由于孩子过程以不洁的方式终止而引起的吗?还有什么可能是这个问题的原因 – 还有什么其他步骤可以进一步调试呢?
在处理异常方面,我可以明显地看到它,但我假设这意味着我将无法打印到stdout / stderr以执行其余的操作?我可以以某种方式重新连接这些流 – 也许通过将sys.stdout重置为sys .__ stdout__等?在这种情况下,不能写到stdout / stderr不被认为是致命的,但如果它是一个事情出现错误的迹象,我宁愿提前保释.
我猜最终我有一点损失,在哪里开始调试这一个…