最简单的方法是计算函数中需要几分钟才能完成的运行时间,然后简单地将该时间打印到shell中。然而,根据您的功能,这可能不是最好的解决方案。在
第二种方法是使用多线程。所以让这个函数在一个线程中运行一段时间,而你的程序则处于一个循环中,每隔一段时间打印出运行的时间,并寻找线程的完成。在
比如:import threading
import time
arg1=0
arg2=1
etc=2
# your function that takes a while.
# Note: If your function returns something or if you want to pass variables in/out,
# you have to use Queues
def yourFunction(arg1,arg2,etc):
time.sleep(10) #your code would replace this
# Setup the thread
processthread=threading.Thread(target=yourFunction,args=(arg1,arg1,etc)) #set the target function and any arguments to pass
processthread.daemon=True
processthread.start() # start the thread
#loop to check thread and display elapsed time
while processthread.isAlive():
print time.clock()
time.sleep(1) # you probably want to only print every so often (i.e. every second)
print 'Done'
然后,你可以通过重写shell中的时间来获得更好的效果,或者使用gui来显示进度条!在