Is output buffering enabled by default in Python's interpreter for sys.stdout
?
If the answer is positive, what are all the ways to disable it?
Suggestions so far:
- Use the
-u
command line switch - Wrap
sys.stdout
in an object that flushes after every write - Set
PYTHONUNBUFFERED
env var sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
Is there any other way to set some global flag in sys
/sys.stdout
programmatically during execution?
I would rather put my answer in How to flush output of Python print? or in Python's print function that flushes the buffer when it's called?, but since they were marked as duplicates of this one (what I do not agree), I'll answer it here.
Since Python 3.3 print() supports the keyword argument "flush" (see documentation):
You can create an unbuffered file and assign this file to sys.stdout.
You can't magically change the system-supplied stdout; since it's supplied to your python program by the OS.
(I've posted a comment, but it got lost somehow. So, again:)
As I noticed, CPython (at least on Linux) behaves differently depending on where the output goes. If it goes to a tty, then the output is flushed after each '
\n'
If it goes to a pipe/process, then it is buffered and you can use the
flush()
based solutions or the -u option recommended above.Slightly related to output buffering:
If you iterate over the lines in the input with
for line in sys.stdin:
...
then the for implementation in CPython will collect the input for a while and then execute the loop body for a bunch of input lines. If your script is about to write output for each input line, this might look like output buffering but it's actually batching, and therefore, none of the
flush()
, etc. techniques will help that. Interestingly, you don't have this behaviour in pypy. To avoid this, you can usewhile True: line=sys.stdin.readline()
...
In Python 3, you can monkey-patch the print function, to always send flush=True: