我刚刚了解到SIGPIPE,然后阅读了关于如何在Python中处理它们的内容。
除其他来源外,我还阅读了:如何在Python中处理断开的管道(SIGPIPE)?
假设读取管道的脚本退出,那么所有答案都建议编写脚本的写入调用应该包含在try子句中。
然而,我无法使其正常工作。这是我的代码:
# printer.py
import time
try:
for i in range(10):
time.sleep(1)
print i
except:
print 'We caught the problem'
并且
#nonreader.py
#read nothing, but stay alive for 5 sec
import time, sys
time.sleep(5)
sys.exit(0)
在命令行中:
$ python printer.py | python nonreader.py
close failed in file object destructor:
Error in sys.excepthook:
Original exception was:
显然,没有捕获到任何异常。而且,当它打印“原始异常是:”后就没有了,看起来非常不对劲。
我错在哪里/我理解错了什么?
Thomas