我想查看程序在Spyder中运行时的进度。可能吗?截至目前,我似乎不知道它什么时候结束,除非我在底部写一个print语句表明程序已经完成执行
答案 0 :(得分:5)
我经常在巨型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();
希望有所帮助。我确信这个网站上更复杂的程序员有更优雅的方法来做这些事情。