RepeatTimer.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.t=t
  7. self.hFunction = hFunction
  8. self.thread = Timer(self.t,self.handle_function)
  9. def handle_function(self):
  10. self.hFunction()
  11. self.thread = Timer(self.t,self.handle_function)
  12. self.thread.start()
  13. def start(self):
  14. self.thread.start()
  15. def cancel(self):
  16. self.thread.cancel()
  17. @property
  18. def function(self):
  19. return self.hFunction
  20. @function.setter
  21. def function(self,value:callable):
  22. self.hFunction = value
  23. @property
  24. def Interval(self):
  25. return self.thread.interval
  26. @Interval.setter
  27. def Interval(self, value:float):
  28. self.thread.interval = value
  29. @property
  30. def daemon(self):
  31. return self.thread.daemon
  32. @daemon.setter
  33. def daemon(self, value:bool):
  34. self.thread.daemon = value
  35. def setDaemon(self, value:bool):
  36. self.thread.setDaemon(value)
  37. @property
  38. def function(self):
  39. return self.thread.function
  40. @function.setter
  41. def function(self, value):
  42. self.thread.function = value
  43. @property
  44. def name(self):
  45. return self.thread.name
  46. @name.setter
  47. def name(self, value:str):
  48. #self.thread.name = value
  49. self.thread.setName(value)
  50. def setName(self,value:str):
  51. self.thread.setName(value)