-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_interval.py
More file actions
66 lines (55 loc) · 2.21 KB
/
async_interval.py
File metadata and controls
66 lines (55 loc) · 2.21 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import asyncio
import types
# Calls fn in intervals given by the interval parameter (in seconds)
# fn is called with an argument giving the elapsed time since start
# Returns a Task object
def async_interval(fn, interval, end = 0):
async def run(fn, interval):
try:
start = loop.time()
while True:
await asyncio.sleep(interval)
fn( loop.time() - start )
except asyncio.CancelledError:
pass
def stop():
try: task.cancel()
except asyncio.CancelledError: pass
loop = asyncio.get_running_loop()
task = asyncio.create_task( run(fn, interval) )
if (end > 0): loop.call_later(end, stop)
return task
if __name__ == '__main__':
import unittest
class Test(unittest.IsolatedAsyncioTestCase):
async def test_cancel(self):
def empty(): pass
i = async_interval(empty, 1)
await asyncio.sleep(0) # need to wait, otherwise CancelledError is raised anyway
i.cancel()
await i # wait for interval completion
self.assertEqual(i.done(), True)
# cancelled() is False since the wrapped coroutine doesn't propagate the CancelledError
self.assertEqual(i.cancelled(), False)
async def test_interval(self):
times = []
def fn(time): times.append(time)
i = async_interval(fn, 1/4)
await asyncio.sleep(1)
i.cancel()
await i
# print(times)
self.assertEqual(len(times), 3)
self.assertAlmostEqual(times[0], 0.25, places=2)
self.assertAlmostEqual(times[1], 0.50, places=2)
self.assertAlmostEqual(times[2], 0.75, places=2)
async def test_end(self):
times = []
def fn(time): times.append(time)
i = async_interval(fn, 1/4, 1)
await i
self.assertEqual(len(times), 3)
self.assertAlmostEqual(times[0], 0.25, places=2)
self.assertAlmostEqual(times[1], 0.50, places=2)
self.assertAlmostEqual(times[2], 0.75, places=2)
unittest.main()