-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
58 lines (42 loc) · 1.13 KB
/
example.py
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
import asyncio
# cancelable infinity loop
async def func1(token):
while not token.is_cancelled:
await asyncio.sleep(1)
return "complete func1."
# uncancelable limited loop
async def func2(token):
for i in range(10):
await asyncio.sleep(1)
return f"complete func2. result: {i}"
# force cancel infinity loop
async def func3():
while True:
await asyncio.sleep(1)
return "complete func3. unreachable code."
# uncancelable limited loop
def func4():
for i in range(1000):
...
return f"complete func4. result: {i}"
async def func5():
try:
while True:
await asyncio.sleep(1)
except asyncio.CancelledError:
print("occured asyncio.CancelledError.")
# from callable
class YourDeamon:
def __init__(self, value):
self.value = value
async def __call__(self, token):
value = self.value
while not token.is_cancelled:
await asyncio.sleep(1)
return f"complete func5. result: {value}"
func6 = YourDeamon(1)
# Do not run
# infinity loop
# async def func7():
# while True:
# print("waiting")