次のコードは、ティーにパイプするとパイプが壊れて終了しますが、パイプされていない場合は正しく動作します。
#!/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 にパイプされたときに観察されるのは、パイプが壊れているか、出力がまったくないことです。つまり、ティー stdout には何もなく、 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
この理由は何ですか?