python实时打印功能 [英] python print function in real time
问题描述
我最近更换了操作系统并使用了更新的 Python (2.7).在我的旧系统上,我曾经能够即时打印.例如,假设我有一个计算量很大的 for 循环:
for i in range(10):巨大的计算打印我
然后当代码完成每次迭代时,它会打印 i
但是,在我当前的系统上,python 似乎缓存了标准输出,以便终端在几分钟内空白,然后打印:
<前>123在短时间内.然后,再过几分钟,它会打印:
<前>456等等.如何在到达 print
语句后立即进行 python 打印?
打印后尝试调用flush of stdout
导入系统...sys.stdout.flush()
或者使用 命令行 选项 -u 其中:
<块引用>强制标准输入、标准输出和标准错误完全无缓冲.
I recently switched OS and am using a newer Python (2.7). On my old system, I used to be able to print instantaneously. For instance, suppose I had a computationally intense for loop:
for i in range(10):
huge calculation
print i
then as the code completed each iteration, it would print i
However, on my current system, python seems to cache the stdout so that the terminal is blank for several minutes, after which it prints:
1 2 3
in short succession. Then, after a few more minutes, it prints:
4 5 6
and so on. How can I make python print as soon as it reaches the print
statement?
Try to call flush of stdout after the print
import sys
...
sys.stdout.flush()
Or use a command line option -u which:
Force stdin, stdout and stderr to be totally unbuffered.
这篇关于python实时打印功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!