[英]Redirecting stdout of a python code to a file WITHOUT letting python to keep it locked
I am trying the most simple method of redirecting the stdout
of a python code to a file using a command such as below. 我正在尝试使用以下命令将python代码的
stdout
重定向到文件的最简单方法。
python foo.py > SmokeyStover.log
This works fine except I need to stop my python code to be able to read the added logs into this log file and the result for commands such as 除我需要停止我的python代码以能够将添加的日志读入该日志文件以及命令的结果(例如,
tail -f SmokeyStover.log
while the code is running, using the python interpreter, is no incoming logs in the real time stream of logs getting appended to this file continuously. 当代码运行时,使用python解释器,在实时日志流中没有传入日志不断被追加到此文件中。
I also used other approaches to redirect the stdio
inside the python code using commands such as following and the file simply doesn't get flushed. 我还使用其他方法使用以下命令来重定向python代码中的
stdio
,例如以下命令,并且文件不会被刷新。
sys.stdout = open('SmokeyStover.log', 'a')
Probably you will have to flush the buffer manually, for instance: 可能您将不得不手动刷新缓冲区,例如:
import sys
import time
for i in range(100):
print "number is %s " %i
time.sleep(1)
sys.stdout.flush()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.