Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这种显示函数进度的方式有什么问题?_Python_Ipython - Fatal编程技术网

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是终端,现在可以正常工作了。感谢您帮助
打印
,不带尾随逗号隐式刷新标准输出。