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).
Recommended Posts