2011-05-02 82 views
0

這裏是我的功能這種顯示函數進度的方式有什麼問題?

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()來運行該功能, 但似乎進展表明 直到整個函數結束

====== ================= 現在我又試了一次,好像沒事......

+0

工作,當你在外面跑它什麼IPython的? – 2011-05-02 08:02:55

+0

對我來說工作很好(我通過'sleep(1)'定義了'work()')。什麼是stdout連接到您的過程?終端或文件?你的平臺是什麼? – 2011-05-02 08:03:50

+0

stdout是終端,它現在工作正常。感謝您幫助 – zjk 2011-05-02 08:09:39

回答

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 
+2

'print'沒有後綴逗號隱式刷新標準輸出。 – 2011-05-02 08:10:38

0

在python解釋器中,可以使用-u選項關閉輸出的緩衝(儘管在ipython中該選項有不同的含義)。

只需調用腳本就像

python -u script.py 

可以放置在script.py本身的選項:

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

import os 
... 

希望它會:-)