8

How often does sys.stderr flush its buffer, and is this standard among different environments?

>>> import sys
>>> sys.__stderr__
<open file '<stderr>', mode 'w' at 0x2b4fcb7ac270>

I see that it is just a standard file type, but I don't know what value of buffering it's supposed to be. dir() does not seem to yield any useful information either.

Brian
  • 132
  • 1
  • 9
  • 1
    stderr, independent of `sys.stderr`, is usually unbuffered. – Ignacio Vazquez-Abrams Mar 18 '14 at 05:28
  • Just came across a use case where stderr is buffered and it causes problems: `docker run -d -p 8000:8000 ubuntu python3 -m http.server` runs a python HTTP server daemon. All output is to stderr and python will not flush it to `docker logs` without the `-u` flag given in the answer. – Air Sep 23 '14 at 14:46

1 Answers1

5

Update: sys.stderr is line-buffered by default since Python 3.9. If python receives -u command-line option or $PYTHONUNBUFFERED environment variable is set, then stdout and stderr streams are forced to be unbuffered (including text layer since Python 3.7).


old answer:

On Python 2, I can't find where in the documentation sys.stderr's buffering is specified. I'd expect the same behaviour as stderr in C that is unbuffered (except Windows) and I don't know whether c99 standard mandates it. The standard error stream is not fully buffered in POSIX. -u option forces standard streams to be unbuffered in Python 2.

On Python 3:

When interactive, standard streams are line-buffered. Otherwise, they are block-buffered like regular text files. You can override this value with the -u command-line option.

-u command-line option:

Force the binary layer of the stdout and stderr streams (which is available as their buffer attribute) to be unbuffered. The text I/O layer will still be line-buffered if writing to the console, or block-buffered if redirected to a non-interactive file.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Important change: Changed in version 3.7: The text layer of the stdout and stderr streams now is unbuffered. – user582175 Oct 21 '20 at 12:25