Nohup和Python -u:它仍然不会实时记录数据

Bas*_*asj 5 python stdout nohup

在后台启动Python进程时,用

nohup python myscript.py > test.log 2>&1 < /dev/null &
Run Code Online (Sandbox Code Playgroud)

问题是stdout缓冲:数据不是实时写入的test.log.以常见的解决办法这个问题定时冲洗sys.stdout.flush(),甚至更好,因为提出这个答案,使用python -u:

nohup python -u myscript.py > test.log 2>&1 < /dev/null &
Run Code Online (Sandbox Code Playgroud)

但这还不够.我注意到它在几个小时内工作,然后,它停止工作,即几个小时后test.log不再实时写入,即使我使用过nohup python -u ....

1)这是如何重现问题(我有一个标准的Debian Jessie).启动此文件:

import time
import datetime
while True:
    print datetime.datetime.now()
    time.sleep(60)
Run Code Online (Sandbox Code Playgroud)

nohup python -u myscript.py > test.log 2>&1 < /dev/null &.日志文件将在几个小时内更新,然后在3或4小时后更新,不再有任何内容.

2)如何解决这个问题,而不必stdout.flush()在代码中插入每两行(丑陋的解决方案)?

Cod*_*ody 2

如果 python -u 似乎不适合您,下一个建议是将 sys.stdout 替换为不缓冲输出的自定义类。

Magnus Lycka 在邮件列表中提供了此解决方案

class Unbuffered(object):
    def __init__(self, stream):
        self.stream = stream
    def write(self, data):
        self.stream.write(data)
        self.stream.flush()
    def __getattr__(self, attr):
        return getattr(self.stream, attr)

import sys
sys.stdout = Unbuffered(sys.stdout)
print 'Unbuffered Output'
Run Code Online (Sandbox Code Playgroud)

我不明白为什么 python -u 从你的例子中不起作用,但这应该可以为你解决问题。