Search code examples
pythontee

python stdout flush and tee


The following code ends with broken pipe when piped into tee, but behave correctly when not piped :

#!/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()

Expected output :

./bug.py 
Ctrl-C pressed

What is observed when piped into tee is either a broken pipe or no output at all, ie nothing on tee stdout, and nothing in 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

What can be the reason for this ?


Solution

  • When you hit Ctrl-C, the shell will terminate both processes (python and tee) and tear down the pipe connecting them.

    So when you handle the Ctrl-C in your python process and flush, it'll find that tee has been terminated and the pipe is no more. Hence the error message.

    (As an aside, would you expect anything in the log ? I don't see your process outputting anything other than the flush on exit)