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

How can I view the progress of a program run in spyder IDE?

我想在 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(); 

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