Nohup 和 Python -u :它仍然没有实时记录数据 [英] Nohup and Python -u : it still doesn't log data in realtime

查看:25
本文介绍了Nohup 和 Python -u :它仍然没有实时记录数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在后台启动 Python 进程时,使用

When launching a Python process, in background, with

nohup python myscript.py > test.log 2>&1 < /dev/null &

问题在于stdout 被缓冲:数据没有实时写入test.log.这个问题的常见解决方案是使用sys.stdout.flush() 定期刷新,甚至更好,如建议的通过这个答案,使用 python -u :

the problem is that stdout is buffered: the data is not written in realtime to test.log. The common solution to this problem is to flush periodically with sys.stdout.flush(), or even better, as suggested by this answer, to use python -u :

nohup python -u myscript.py > test.log 2>&1 < /dev/null &

<小时>

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


But this is not enough. I noticed that it worked during a few hours, and then, it stopped working, i.e. after a few hours test.log is not written in realtime anymore, even if I used nohup python -u ....

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

1) This is how to reproduce the problem (I have a standard Debian Jessie). Start this file:

import time
import datetime
while True:
    print datetime.datetime.now()
    time.sleep(60)

使用 nohup python -u myscript.py >test.log 2>&1</dev/null &.日志文件将在几个小时内更新,然后在 3 或 4 小时后,什么都没有了.

with nohup python -u myscript.py > test.log 2>&1 < /dev/null &. The log file will be updated during a few hours, and then after 3 or 4 hours, nothing anymore.

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

2) How to solve this problem, without having to insert stdout.flush() every 2 lines in the code (ugly solution) ?

推荐答案

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

If python -u does not seem to be working for you, the next suggestion would be to replace sys.stdout with a custom class that does not buffer output.

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'

我不明白为什么 python -u 在您的示例中不起作用,但这应该可以为您解决问题.

I can't see why python -u wouldn't work from your example, but this should solve the issue for you.

这篇关于Nohup 和 Python -u :它仍然没有实时记录数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆