Jan-03-2017, 01:15 PM
I'll have to scratch my butt a bit in order to figure out where the hair-brained idea came from
It looks like I used the timeout as my clock.
This is not quite there, perhaps you can fix it.
I need to take about a three hour nap (happens when you get old), then I'll be back
This is close to being right
It looks like I used the timeout as my clock.
This is not quite there, perhaps you can fix it.
I need to take about a three hour nap (happens when you get old), then I'll be back
This is close to being right
# # Author: Larry McCaig (Larz60+) # No warranty, use at your own risk! # Modify if you wish # import threading class EventTimer: ''' Creates overlaping threaded timed events Events can be set to trigger in days, hours, minutes, and/or seconds Any number of events can be created, and are treated independently, thus can overlap each other. Program execution will continue until all events have completed. the function that is passed to each timer will execute after the timeout has been reached ''' def __init__(self): self.threadList = [] def addEvent(self, func, days=0, hours=0, minutes=0, seconds=0, new=False): if new: self.threadList = [] times = [days, hours, minutes, seconds] multiplier = [86400, 3600, 60, 1] count = 0 for n in range(len(times)): count += times[n] * multiplier[n] t = threading.Timer(count, func) self.threadList.append(t) def startAll(self): # Start threads [th.start() for th in self.threadList] def waitForFinish(self): # wait for all to finish [th.join() for th in self.threadList] class MyTimedEvent(EventTimer): def __init__(self, num_iters=1, seconds=0): self.num_iterations_so_far = 0 self.num_iters = num_iters self.seconds = seconds self.kick_off_event() def kick_off_event(self): self.addEvent(self.event_triggered, seconds=self.seconds, new=True) def event_triggered(self): self.num_iters -= 1 if self.num_iters: kick_off_event() self.timed_event() def timed_event(self): self.num_iterations_so_far += 1 print('Processed {}'.format(self.num_iterations_so_far)) def launch(): mte = MyTimedEvent(num_iters=8, seconds=10) mte.kick_off_event() mte.waitForFinish() if __name__ == '__main__': launch()