asyncio_base.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. Eventloop for integration with Python3 asyncio.
  3. Note that we can't use "yield from", because the package should be installable
  4. under Python 2.6 as well, and it should contain syntactically valid Python 2.6
  5. code.
  6. """
  7. from __future__ import unicode_literals
  8. __all__ = (
  9. 'AsyncioTimeout',
  10. )
  11. class AsyncioTimeout(object):
  12. """
  13. Call the `timeout` function when the timeout expires.
  14. Every call of the `reset` method, resets the timeout and starts a new
  15. timer.
  16. """
  17. def __init__(self, timeout, callback, loop):
  18. self.timeout = timeout
  19. self.callback = callback
  20. self.loop = loop
  21. self.counter = 0
  22. self.running = True
  23. def reset(self):
  24. """
  25. Reset the timeout. Starts a new timer.
  26. """
  27. self.counter += 1
  28. local_counter = self.counter
  29. def timer_timeout():
  30. if self.counter == local_counter and self.running:
  31. self.callback()
  32. self.loop.call_later(self.timeout, timer_timeout)
  33. def stop(self):
  34. """
  35. Ignore timeout. Don't call the callback anymore.
  36. """
  37. self.running = False