1. shell 环境变量
- 执行
export PYTHONUNBUFFERED=1
, 那么再此shell下源码
执行的 nohup python xx.py &
将不再将日志刷新到缓冲区而是直接输出到 nohup.out
2. python -u
nohup python -u xx.py &
, 日志将直接输出到 nohup.out ,不走缓冲区
3. flush buffer (非源码部署适用
)
- 强刷 sys.write 的缓冲区,
print
的标准输出实际上是到了 sys.write的缓冲区,当遇到回车
、 程序执行结束、 flush 或者 达到了指定的大小 等操作 则会输出到控制台或者 nohup.out, 代码更改
import sys
class UnBuffer:
def __init__(self, stream):
slef.stream = stream
def write(self, char):
self.stream.write()
self.stream.flush()
def __getattr__(self, attr)
return getattr(self.stream, attr)
sys.stdout = UnBuffer(sys.stdout)