[Note] [One-line memo] If you add the -u option to Python, stdout and stderr become unbuffered. Useful when piped to a tee

The title is all about the content, but for the time being

python -c 'import time; print(1); time.sleep(1); print(2)

⇒ 1 is displayed, and after a while, 2 is displayed.

sh -c 'echo 1; sleep 1; echo 2'

⇒ 1 is displayed, and after a while, 2 is displayed.

python -c 'import time; print(1); time.sleep(1); print(2) | tee /dev/null

⇒ After a while, 1 and 2 are displayed at once

sh -c 'echo 1; sleep 1; echo 2' | tee /dev/null

⇒ 1 is displayed, and after a while, 2 is displayed.

Why does it behave differently only when piped in Python?

Python is smart, so if stdout and stderr are terminals, they will buffer the line buffer, otherwise they will buffer normally. (When outputting a lot, buffering is overwhelmingly faster) By the way, Python does it itself, so the stdbuf command can't change its behavior.

With the -u option, it becomes unbuffered.

python -u -c 'import time; print(1); time.sleep(1); print(2) | tee

⇒ 1 is displayed, and after a while, 2 is displayed.

Recommended Posts

[Note] [One-line memo] If you add the -u option to Python, stdout and stderr become unbuffered. Useful when piped to a tee
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
Write a script in Shell and Python to notify you in Slack when the process is finished
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
Python Note: When you want to know the attributes of an object
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
A useful note when using Python for the first time in a while
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3
Option to disable stdout / stderr buffers in Python
Python Note: When assigning a value to a string
A memo of misunderstanding when trying to load the entire self-made module with Python3