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