【发布时间】:2017-09-30 16:06:16
【问题描述】:
背景:我注意到技巧nohup python test.py & 通常不起作用,因为输出未实时正确保存在nohup.out 中。给出in this famous question(如nohup python -u)的解决方案并不总是有效,如图in my other question here。
问题:我的代码中到处都使用了print(...),所以我不想更改它并用另一个日志记录函数替换它。是否可以重新定义print 来代替:
from __future__ import print_function
def print(s):
with open('out.txt', 'a+') as f:
f.write(str(s) + '\n')
old_print(s) # how to print it with the standard print too?
【问题讨论】:
-
您之前问题的第一个答案有什么问题?
-
@user2357112 如果我没记错的话,
flush的解决方案对我来说在 100% 的情况下都不起作用。 -
如果
flush对您不起作用,那么没有理由期望close会更好地工作。您可能应该调查 nohup 问题的根本原因;您现在所做的基本上是shot弹枪调试。 -
打开、写入、关闭对我来说都可以(已经测试过),然后我会 100% 确定它会起作用。
标签: python python-2.7 logging nohup