Dynamic Output of time progression
-
20-12-2019 - |
Question
I have seen many suggestions on how to show a progress bar based on iteration in Python. My question is is there a way to do this based on time?
Use Case
func()
is not-deterministic function whose run-time varies from seconds to nearly 30 minutes. I want as many results as I can gather in the space of an hour and therefore am running func()
as many times as I can in the space of an hour. As such, I don't want to print an update in the tradition way as I don't have any way of knowing if it is stuck or just on a long case (both happen).I don't have control of func()
or I would try to alleviate some of these misfortunes. As such I would like something as simple as a countdown from an hour that updates every 5 minutes, regardless of the runtime of func()
as a sign of life.
results = []
T1, T2 = time.time(), time.time()
while T2 - T1 < 1 * 60 * 60:
results.append( func() )
T2 = time.time()
My initial thoughts would be to do something with threading or pools.
Solution
old_stdout = sys.stdout
sys.stdout = open("log.txt","w") # now calling print will write to this logfile
results = []
T1, T2 = time.time(), time.time()
while T2 - T1 < 1 * 60 * 60:
results.append( func() )
T2 = time.time()
old_stdout.write("%0.2f%% Complete\n"%(100.0*(T2-T2)/(1.0*60*60)))
would redirect your normal stdout to a log file, and then print percent complete status messages after each run
it sounds like you want a thread
import time,threading,random
class func_thread(threading.Thread):
def __init__(self,callback):
self.callback = callback
self.killed = False
threading.Thread.__init__(self)
def Kill(self):
self.killed = True
def run(self):
while not self.killed:
self.callback(func())
def func():
time.sleep(random.uniform(0.5,2.0))
return 5
seconds = 1
minutes = 60
update_interval = 25 * seconds
done_time = 5 * minutes
update_format ="# Results:%04d Seconds Elapsed:%0.3f"
def main():
my_results = []
start_time = time.time()
fs = func_thread(my_results.append)
fs.start()
while time.time() - start_time < done_time:
print update_format%(len(my_results),time.time()-start_time)
time.sleep(update_interval)
fs.Kill()
fs.join()
print "DONE FINAL COUNT:",len(my_results)