Detailed explanation of -u parameter of python command

origin:

Today, I was looking at the training code of arcface. When running the python command in shellscript, the -u parameter (python -u xx.py) was added, so I did a little research on this parameter.

Prepare knowledge

Use a [program] (https://www.zalou.cn/tag/chengxu) example on the Internet to illustrate the output rules of standard error (std.err) and standard output (std.out) in python (standard output needs to be cached by default before output to the screen, while standard error is Print directly to the screen):

import sys
 
sys.stdout.write("stdout1")
sys.stderr.write("stderr1")
sys.stdout.write("stdout2")
sys.stderr.write("stderr2")

Among them, sys.stdout.write() and sys.stderr.write() are statements that print to the screen. In fact, the print statement in python calls sys.stdout.write(). For example, when the print object calls print obj, it actually calls sys.stdout.write(obj+'n').

**The expected result is **

stdout1stderr1stdout2stderr2

**The actual result is **

stderr1stderr2stdout1stdout2

The reason is the python cache mechanism. Although both stderr and stdout point to the screen by default, stderr is not cached. Program outputs a character to stderr, and one character will be displayed on the screen; and stdout is cached , It will be displayed only when it encounters a line break or accumulates to a certain size. This is why the above will show two stderr first.

- Use of u parameters

With the above foreshadowing, the -u parameter of python can be derived. The python command plus the -u (unbuffered) parameter will force its standard output to be printed directly to the screen without buffering, just like standard error.

operation result

stdout1stderr1stdout2stderr2

This becomes the expected output.

**Note: **The above results are implemented under python2, I also tested under python3, even with -u or environment variable UNBUFFERED=1 under python3, stdout still writes the cache (execution result stderr1stderr2stdout1stdout2), The specific reason is not clear, and I will update it after finding it out.

Through the above analysis, it is not difficult to see that especially in the case of the python execution script output to the screen and the result is directly redirected to the log file, the -u parameter is used, so that the result of the standard output is output directly to the log file without being cached Log file.

The above detailed explanation of the -u parameter of the python command is all the content shared by the editor. I hope to give you a reference, and I hope you can support website (zalou.cn).

Articles you may be interested in:

Recommended Posts

Detailed explanation of -u parameter of python command
Detailed explanation of the principle of Python function parameter classification
Detailed explanation of python backtracking template
Detailed explanation of python sequence types
Detailed explanation of Python IO port multiplexing
Detailed explanation of Python guessing algorithm problems
Detailed explanation of the principle of Python super() method
Detailed explanation of python standard library OS module
Detailed explanation of the usage of Python decimal module
Detailed explanation of how python supports concurrent methods
Detailed explanation of data types based on Python
Detailed explanation of the principle of Python timer thread pool
Detailed explanation of the implementation steps of Python interface development
Detailed explanation of common tools for Python process control
Detailed explanation of Python web page parser usage examples
Detailed explanation of the attribute access process of Python objects
Detailed explanation of the remaining problem based on python (%)
Python method of parameter passing
Detailed implementation of Python plug-in mechanism
Detailed explanation of ubuntu using gpg2
Python function definition and parameter explanation
Python error handling assert detailed explanation
Detailed usage of dictionary in Python
Detailed analysis of Python garbage collection mechanism
Python from attribute to property detailed explanation
Detailed explanation of the use of pip in Python | summary of third-party library installation
Ubuntu20.04 install Python3 virtual environment tutorial detailed explanation
7 features of Python3.9
Detailed explanation of building Hadoop environment on CentOS 6.5
Detailed examples of using Python to calculate KS
Detailed explanation of static DNS configuration method in Ubuntu
Detailed explanation of Centos 7 system virtual machine bridging mode
Detailed explanation of static DNS configuration under Ubuntu system
Equal Insurance Evaluation: Detailed Explanation of Centos Timeout Exit
Detailed explanation of CentOS7 network setting tutorial in vmware
Detailed Python IO programming
Detailed Python loop nesting
Basics of Python syntax
Basic syntax of Python
Python—requests module detailed explanation
Prettytable module of python
09. Common modules of Python3
Detailed explanation of Spark installation and configuration tutorial under centOS7