RepeatTimer.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from threading import Timer,Thread,Event
  4. class RepeatTimer():
  5. def __init__(self,t:float,hFunction:callable):
  6. self.__deamon = True
  7. try:
  8. self.t=t
  9. self.hFunction = hFunction
  10. self.thread = Timer(self.t,self.handle_function)
  11. self.thread.daemon = self.__deamon
  12. except (KeyboardInterrupt, SystemExit):
  13. self.thread.cancel()
  14. self.thread.release()
  15. def handle_function(self):
  16. self.hFunction()
  17. try:
  18. self.thread = Timer(self.t,self.handle_function)
  19. self.thread.daemon = self.__deamon
  20. self.thread.start()
  21. except (KeyboardInterrupt, SystemExit):
  22. self.thread.cancel()
  23. self.thread.release()
  24. def start(self):
  25. self.thread.start()
  26. def cancel(self):
  27. self.thread.cancel()
  28. @property
  29. def function(self):
  30. return self.hFunction
  31. @function.setter
  32. def function(self,value:callable):
  33. self.hFunction = value
  34. @property
  35. def Interval(self):
  36. return self.thread.interval
  37. @Interval.setter
  38. def Interval(self, value:float):
  39. self.thread.interval = value
  40. @property
  41. def daemon(self):
  42. return self.thread.daemon
  43. @daemon.setter
  44. def daemon(self, value:bool):
  45. self.__deamon = value
  46. self.thread.daemon = value
  47. def setDaemon(self, value:bool):
  48. self.thread.setDaemon(value)
  49. @property
  50. def function(self):
  51. return self.thread.function
  52. @function.setter
  53. def function(self, value):
  54. self.thread.function = value
  55. @property
  56. def name(self):
  57. return self.thread.name
  58. @name.setter
  59. def name(self, value:str):
  60. #self.thread.name = value
  61. self.thread.setName(value)
  62. def setName(self,value:str):
  63. self.thread.setName(value)