以下代码在通过管道传输到 tee 时以损坏的管道结尾,但在未通过管道传输时行为正确:
#!/usr/bin/python
import sys
def testfun():
while 1:
try :
s = sys.stdin.readline()
except(KeyboardInterrupt) :
print('Ctrl-C pressed')
sys.stdout.flush()
return
print s
if __name__ == "__main__":
testfun()
sys.exit()
预期输出:
./bug.py
Ctrl-C pressed
当管道进入 tee 时观察到的是管道损坏或根本没有输出,即 tee 标准输出上没有任何内容,而 bug.log 中没有任何内容:
./bug.py | tee bug.log
Traceback (most recent call last):
File "./bug.py", line 14, in <module>
sys.stdout.flush()
IOError: [Errno 32] Broken pipe
这可能是什么原因?