相关疑难解决方法(0)

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

在后台启动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()在代码中插入每两行(丑陋的解决方案)?

python stdout nohup

5
推荐指数
1
解决办法
1144
查看次数

标签 统计

nohup ×1

python ×1

stdout ×1