Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. from threading import Timer
  2. import time
  3.  
  4.  
  5. class ResettableTimer(object):
  6. def __init__(self, interval, function):
  7. self.interval = interval
  8. self.function = function
  9. self.timer = Timer(self.interval, self.function)
  10.  
  11. def run(self):
  12. self.timer.start()
  13.  
  14. def reset(self):
  15. self.timer.cancel()
  16. self.timer = Timer(self.interval, self.function)
  17. self.timer.start()
  18.  
  19.  
  20. if __name__ == '__main__':
  21. t = time.time()
  22. tim = ResettableTimer(5, lambda: print("Time's Up! Took ", time.time() - t, "seconds"))
  23. time.sleep(3)
  24. tim.reset()
  25.  
  26. import threading
  27. import time
  28.  
  29. def hi( ):
  30. print('hi')
  31. mine.start()
  32.  
  33.  
  34.  
  35. class ReusableTime():
  36. def __init__(self, t, func):
  37. self._t = t
  38. self._func = func
  39.  
  40. def start(self):
  41. self._thread = threading.Timer(self._t, self.handler)
  42. self._thread.start()
  43.  
  44. def handler(self):
  45. self._func()
  46.  
  47.  
  48. mine = ReusableTime(2, hi)
  49. mine.start()
  50. time.sleep(100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement