这种显示功能进展的方式有什么问题?

时间:2011-05-02 07:58:59

标签: 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()来运行这个函数, 但似乎显示了进展 直到整个功能结束

======================= 我再试一次,现在看来还好......

2 个答案:

答案 0 :(得分:0)

尝试强制刷新标准输出:

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

答案 1 :(得分:0)

在python解释器中,可以使用-u选项关闭输出的缓冲(虽然此选项在ipython中具有不同的含义)。

只需像

一样调用您的脚本
python -u script.py

您可以将选项放在script.py本身:

#!/usr/bin/python -u
# -*- coding: UTF-8 -*-

import os
...

希望它能起作用: - )