forked from peterhinch/micropython-async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyn_demos.py
131 lines (113 loc) · 4.23 KB
/
asyn_demos.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# asyn_demos.py Simple demos of task cancellation
# Test/demo of official asyncio library and official Lock class
# The MIT License (MIT)
#
# Copyright (c) 2017 Peter Hinch
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import uasyncio as asyncio
import asyn
def print_tests():
st = '''Minimal demo programs of uasyncio task cancellation.
Issue ctrl-D to soft reset the board between test runs.
Available demos:
cancel_test() Demo of Cancellable tasks.
named_test() Demo of NamedTask.
method_test() Cancellable and NamedTask coros as bound methods.
'''
print('\x1b[32m')
print(st)
print('\x1b[39m')
print_tests()
# Cancellable task minimal example
@asyn.cancellable
async def print_nums(num):
while True:
print(num)
num += 1
await asyn.sleep(1)
@asyn.cancellable
async def add_one(num):
num += 1
await asyn.sleep(1)
return num
async def run_cancel_test(loop):
res = await asyn.Cancellable(add_one, 41)
print('Result: ', res)
loop.create_task(asyn.Cancellable(print_nums, res)())
await asyn.sleep(7.5)
# Cancel any cancellable tasks still running
await asyn.Cancellable.cancel_all()
print('Done')
def cancel_test():
loop = asyncio.get_event_loop()
loop.run_until_complete(run_cancel_test(loop))
# NamedTask minimal example
@asyn.cancellable
async def print_nums_named(num):
while True:
print(num)
num += 1
await asyn.sleep(1)
@asyn.cancellable
async def add_one_named(num):
num += 1
await asyn.sleep(1)
return num
async def run_named_test(loop):
res = await asyn.NamedTask('not cancelled', add_one_named, 99)
print('Result: ', res)
loop.create_task(asyn.NamedTask('print nums', print_nums_named, res)())
await asyn.sleep(7.5)
asyn.NamedTask.cancel('not cancelled') # Nothing to do: task has finished
asyn.NamedTask.cancel('print nums') # Stop the continuously running task
print('Done')
def named_test():
loop = asyncio.get_event_loop()
loop.run_until_complete(run_named_test(loop))
# Tasks as bound methods
class CanDemo():
async def start(self, loop):
loop.create_task(asyn.Cancellable(self.foo, 1)()) # 3 instances in default group 0
loop.create_task(asyn.Cancellable(self.foo, 2)())
loop.create_task(asyn.Cancellable(self.foo, 3)())
loop.create_task(asyn.NamedTask('my bar', self.bar, 4)())
print('bar running status is', asyn.NamedTask.is_running('my bar'))
await asyncio.sleep(4.5)
await asyn.NamedTask.cancel('my bar')
print('bar instance scheduled for cancellation.')
await asyn.Cancellable.cancel_all()
print('foo instances have been cancelled.')
await asyncio.sleep(0.2) # Allow for 100ms latency in bar()
print('bar running status is', asyn.NamedTask.is_running('my bar'))
print('Done')
@asyn.cancellable
async def foo(self, arg):
while True:
await asyn.sleep(1)
print('foo running, arg', arg)
@asyn.cancellable
async def bar(self, arg):
while True:
await asyn.sleep(1)
print('bar running, arg', arg)
def method_test():
cantest = CanDemo()
loop = asyncio.get_event_loop()
loop.run_until_complete(cantest.start(loop))