Python 这种显示函数进度的方式有什么问题?
这是我的功能Python 这种显示函数进度的方式有什么问题?,python,ipython,Python,Ipython,这是我的功能 def f(task_list): results = [] for task_id in task_list: results.append(work(task_id)) print len(results) # show the progress return results 我在ipython中使用execfile()来运行此函数, 但似乎进展已经显现 直到整个函数结束 =======================
def f(task_list):
results = []
for task_id in task_list:
results.append(work(task_id))
print len(results) # show the progress
return results
我在ipython中使用execfile()来运行此函数,
但似乎进展已经显现
直到整个函数结束
=======================
我又试了一次,现在似乎没问题…试着强制刷新标准输出:
import sys
def f(task_list):
results = []
for task_id in task_list:
results.append(work(task_id))
print len(results) # show the progress
sys.stdout.flush() # this will force the system to display what is in the stdout queue
return results
在python解释器中,可以使用
-u
选项关闭输出的缓冲(但该选项在ipython中有不同的含义)
就这样称呼你的脚本吧
python -u script.py
您可以将该选项放置在script.py
本身中:
#!/usr/bin/python -u
# -*- coding: UTF-8 -*-
import os
...
希望它能起作用:-)当你在ipython之外运行它的时候呢?对我来说很好(我通过
sleep(1)
定义了work()
)。您的流程的标准输出连接到什么?终端还是文件?你的平台是什么?stdout是终端,现在可以正常工作了。感谢您帮助打印
,不带尾随逗号隐式刷新标准输出。