Python: significance of -u option?
Asked Answered
P

2

72

I am noticed in some python code that -u is used to start the python interpreter. I looked at the man page for python but I could not get much out of it. Please give me some examples.

Phenice answered 10/1, 2013 at 12:58 Comment(1)
I find this very useful when I am launching python from another process or batch and I want to monitor the output continuously instead of large chunks. See https://mcmap.net/q/20922/-disable-output-bufferingJuncture
S
78

From python --help:

-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
         see man page for details on internal buffering relating to '-u'

The manpage states:

-u     Force  stdin,  stdout and stderr to be totally unbuffered.  On systems where it matters, also put stdin,
       stdout and stderr in binary mode.  Note that there is internal buffering  in  xreadlines(),  readlines()
       and  file-object  iterators  ("for  line in sys.stdin") which is not influenced by this option.  To work
       around this, you will want to use "sys.stdin.readline()" inside a "while 1:" loop.

Python opens the stdin, -out and -error streams in a buffered mode; it'll read or write in larger chunks, keeping data in memory until a threshold is reached. -u disables those buffers.

Also, python can interpret newlines on open files and translate them from and to the native platform newlines (text mode). The -u option disables this translation, allowing you to process binary data without having to worry about what might happen to \r\n combinations. It is the equivalent of using rb or wb modes when opening files with the open() function.

Seagirt answered 10/1, 2013 at 12:59 Comment(5)
unbuffered binary stdout and stderr; ??Phenice
For Python3.4, stdin is always buffered, regardless of '-u' usage. (from python3.4 --help)Viand
@JonathanHartley: the manpage may well be incorrect here; note how the --help info (from Python 2.7, I'd say) doesn't mention stdin either. That's because stdin buffering is outside of Python's control.Seagirt
@JonathanHartley: the Python 3 manpages do away with stdin as well. See Turn off buffering in pipe for ways to make stdin unbuffered.Seagirt
Haven't tried this approach but one user reported that -u didn't work for him instead PYTHONUNBUFFERED env worked for me as well him. Is there any difference between two? https://mcmap.net/q/25626/-printing-not-being-logged-by-kubernetes/1306394Whiny
G
30

Python is optimised for reading in and printing out lots of data. One of these optimisation is that the standard input and output of the Python interpreter are buffered. That means that whenever a program tries to use one of those streams, the interpreted will block up the usage into large chunks and then send the chunks all in one go. This is faster than sending each individual read/write through separately, but obviously has the disadvantage that data can get 'stopped up' in the middle.

The -u flag turns off this behaviour.

Guddle answered 10/1, 2013 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.