Unbuffered stderr in Python 3

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Nov 2 02:52:55 EST 2015


In Python 2, stderr is unbuffered.

In most other environments (the shell, C...) stderr is unbuffered.

It is usually considered a bad, bad thing for stderr to be buffered. What 
happens if your application is killed before the buffer fills up? The errors 
in the buffer will be lost.

So how come Python 3 has line buffered stderr? And more importantly, how can 
I turn buffering off?

I don't want to use the -u unbuffered command line switch, because that 
effects stdout as well. I'm happy for stdout to remain buffered.


Here is the function I'm using to test this in in the interactive 
interpreter:

import sys, time

def test():
    # Simulate a slow calculation that prints status and/or error
    # messages to stderr.
    for i in range(10):
        print(i, file=sys.stderr, end="")
        time.sleep(2)
    print("", file=sys.stderr)


Running it pauses for 20 seconds, then displays "0123456789" in one go. What 
I expect is to see the digits arrive individually.

I tried this:

py> sys.stderr.line_buffering = False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: readonly attribute


Next I tried this:

py> sys.stderr.buffer.line_buffering = False


which succeeded, but has no effect on print: it still buffers.




-- 
Steve




More information about the Python-list mailing list