4

I'm running a command like this:

python mycode.py | tee foobar.log 

This code does a lot of "print" to stdout, and without the pipe I can see some output immediately. With the pipe and tee, however, it takes long time until I see the first output, and then there is a lot of it. Looks like some buffer is waiting to be filled, and only when it is full, it dumps everything at once. I'm not sure whether it's tee, python or ubuntu problem. Same problems occurs with ipython for instance. What to do?

Thanks!

komark
  • 197
  • 1
  • 10
  • Flusing stdout might help. Give it a try. – alvits Dec 19 '13 at 00:12
  • 2
    This can't possibly be a duplicate of that other question, since my accepted answer here would be a grossly incorrect answer there (there is of course no direct equivalent in C++ of using a Python command-line option to affect the behavior of the program). The answers there could be adapted for use in Python, but are not the best way to do it in this case. – Steve Jessop Dec 19 '13 at 09:07
  • I agree with @SteveJessop, the accepted answer here adds value to the question beyond the answers in the other question, and I actually use the present answer. The link to other question is useful but not duplicate. – komark Dec 19 '13 at 12:45

1 Answers1

13
python -u mycode.py | tee foobar.log

-u means unbuffered standard input/output. Do check the man page, though. It has one slight catch in Python2 (also puts standard I/O into binary mode, which makes no difference in Ubuntu) and a different slight catch in Python3 (text-level I/O is still line buffered, so if your output doesn't contain many linebreaks you can still see significant buffering).

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699