在后台运行python命令时,会等到缓冲区满或者脚本结束后再输出。如要取消缓存,直接向屏幕输出,解决办法:
- 运行时加-u参数
python3 -u
- 添加环境变量
PYTHONUNBUFFERED=1
- Linux系统中 在print后刷新输出
sys.stdout.flush()
Postscript:
默认向屏幕输出:
stdout – 标准输出
stderr – 标准错误输出
官方文档解释:
-u
Force the stdout and stderr streams to be unbuffered. This option has no effect on the stdin stream.
强制设置无缓冲 stdin、stdout和stderr。
效果与环境变量PYTHONUNBUFFERED相同。
Reference:
https://docs.python.org/3/contents.html