python - 在spyder IDE中如何查看程序运行的进度?

标签 python spyder

我想查看程序在 Spyder 中运行时的进度。是否可以?到目前为止,我似乎不知道它什么时候完成,除非我在底部写一个打印语句来指示程序执行完成

最佳答案

我经常在巨大的 for 循环中添加一个小进度条,让我知道我还要等多久。我假设您编写了正在运行的脚本,因此您可以执行类似的操作。

对于一个非常简单的进度条,它只告诉你它在工作,但不告诉你它走了多远,你可以这样做

# Simple Progress Bar:
import sys  # for progress bar (sys.stdout)

for i in range(1,1000):
    # your loop's complicated code here
    sys.stdout.write('.'); sys.stdout.flush();  # print a small progress bar

(如果您不执行 .flush(),在整个循环完成之前它不会写入任何输出!)

对于一个更复杂的进度条,它实际上告诉我还有多少事情要做,我使用这个代码:

# Full progress bar during long loop:
import sys

scan_wavelengths = range(0,1000000)  # the variable being varied
nWLs = len(scan_wavelengths)  # how many steps in the loop

# Progress Bar setup:
ProgMax = 20    # number of dots in progress bar
if nWLs<ProgMax:   ProgMax = nWLs   # if less than 20 points in scan, shorten bar
print "|" +    ProgMax*"-"    + "|     MyFunction() progress"
sys.stdout.write('|'); sys.stdout.flush();  # print start of progress bar
nProg = 0   # fraction of progress

# The main loop:
for step,wavelength   in   enumerate(scan_wavelengths):
    ''' `step` goes from 0-->N during loop'''

    #<Your complicated stuff in the loop>

    # update progress bar:
    if ( step >= nProg*nWLs/ProgMax ):
        '''Print dot at some fraction of the loop.'''
        sys.stdout.write('*'); sys.stdout.flush();  
        nProg = nProg+1
    if ( step >= nWLs-1 ):
        '''If done, write the end of the progress bar'''
        sys.stdout.write('|     done  \n'); sys.stdout.flush(); 

希望对您有所帮助。我敢肯定,这个网站上许多更精明的程序员有更优雅的方法来做这些事情。

关于python - 在spyder IDE中如何查看程序运行的进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28216660/

相关文章:

python - 保存包含图形的 Spyder 控制台输出

Python qtConsole 和 Spyder : Problems with loading modules

python - Keras 中用于图像分类的 CNN 类型是什么?

python - Poly1d 对象数组的 Numpy 问题

python - 如何根据下拉菜单中的选择动态填充 tkinter 中的选项小部件?

python-3.x - 如何让Spyder标记未使用的变量?

python - 在 Spyder 的 IPython 控制台中使用 awk

python - Python 中 Colorbar 只显示一种颜色

python - 检查 python 列表中的重复项

python - 如何将空列表添加到元组