Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. # encoding: utf-8
  2.  
  3. import time
  4.  
  5. __author__ = 'BetaS'
  6.  
  7.  
  8. class AsyncTimer:
  9. def __init__(self, period=1.0):
  10. self._period = period
  11. self._last_run = 0
  12.  
  13. def check(self):
  14. curr = time.time()
  15. if curr - self._last_run >= self._period:
  16. self._last_run = curr
  17. return True
  18. return False
  19.  
  20. def last_run(self):
  21. return self._last_run
  22.  
  23. def duration(self):
  24. curr = time.time()
  25. return curr - self._last_run
  26.  
  27. def update(self):
  28. self._last_run = time.time()
  29.  
  30.  
  31. if __name__ == "__main__":
  32. t1 = AsyncTimer(1.5) # 1.5s timer
  33. t2 = AsyncTimer(2) # 2s timer
  34.  
  35. while True:
  36. if t1.check():
  37. print("1.5s")
  38. if t2.check():
  39. print("2s")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement