Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from threading import Timer
- import time
- class ResettableTimer(object):
- def __init__(self, interval, function):
- self.interval = interval
- self.function = function
- self.timer = Timer(self.interval, self.function)
- def run(self):
- self.timer.start()
- def reset(self):
- self.timer.cancel()
- self.timer = Timer(self.interval, self.function)
- self.timer.start()
- if __name__ == '__main__':
- t = time.time()
- tim = ResettableTimer(5, lambda: print("Time's Up! Took ", time.time() - t, "seconds"))
- time.sleep(3)
- tim.reset()
- import threading
- import time
- def hi( ):
- print('hi')
- mine.start()
- class ReusableTime():
- def __init__(self, t, func):
- self._t = t
- self._func = func
- def start(self):
- self._thread = threading.Timer(self._t, self.handler)
- self._thread.start()
- def handler(self):
- self._func()
- mine = ReusableTime(2, hi)
- mine.start()
- time.sleep(100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement