1
1
# 1. Guide to uasyncio V3
2
2
3
- The new release of ` uasyncio ` is pre-installed in current daily firmware
4
- builds and will be found in release builds starting with V1.13. This complete
5
- rewrite of ` uasyncio ` supports CPython 3.8 syntax. A design aim is that it
6
- should be be a compatible subset of ` asyncio ` .
3
+ This release of ` uasyncio ` is pre-installed on all platforms except severely
4
+ constrained ones such as the 1MB ESP8266. This rewrite of ` uasyncio ` supports
5
+ CPython 3.8 syntax. A design aim is that it should be be a compatible subset of
6
+ ` asyncio ` . The current version is 3.0.0 .
7
7
8
8
These notes and the tutorial should be read in conjunction with
9
9
[ the official docs] ( http://docs.micropython.org/en/latest/library/uasyncio.html )
@@ -12,7 +12,10 @@ These notes and the tutorial should be read in conjunction with
12
12
13
13
This repo contains the following:
14
14
15
- ### [ V3 Tutorial] ( ./docs/TUTORIAL.md )
15
+ ### [ V3 Tutorial] ( ./docs/TUTORIAL.md )
16
+
17
+ Intended for users with all levels of experience with asynchronous programming.
18
+
16
19
### Test/demo scripts
17
20
18
21
Documented in the tutorial.
@@ -46,6 +49,11 @@ useful in their own right:
46
49
* [ HD44780] ( ./docs/hd44780.md ) Driver for common character based LCD displays
47
50
based on the Hitachi HD44780 controller.
48
51
52
+ ### Event-based programming
53
+
54
+ [ A guide] ( ./docs/EVENTS.md ) to a writing applications and device drivers which
55
+ largely does away with callbacks.
56
+
49
57
### A monitor
50
58
51
59
This [ monitor] ( https://github.com/peterhinch/micropython-monitor ) enables a
@@ -98,9 +106,8 @@ will be addressed in due course.
98
106
### 2.1.1 Fast I/O scheduling
99
107
100
108
There is currently no support for this: I/O is scheduled in round robin fashion
101
- with other tasks. There are situations where this is too slow, for example in
102
- I2S applications and ones involving multiple fast I/O streams, e.g. from UARTs.
103
- In these applications there is still a use case for the ` fast_io ` V2 variant.
109
+ with other tasks. There are situations where this is too slow and the scheduler
110
+ should be able to poll I/O whenever it gains control.
104
111
105
112
### 2.1.2 Synchronisation primitives
106
113
@@ -109,106 +116,3 @@ These CPython primitives are outstanding:
109
116
* ` BoundedSemaphore ` .
110
117
* ` Condition ` .
111
118
* ` Queue ` .
112
-
113
- # 3. Porting applications from V2
114
-
115
- Many applications using the coding style advocated in the V2 tutorial will work
116
- unchanged. However there are changes, firstly to ` uasyncio ` itself and secondly
117
- to modules in this repository.
118
-
119
- ## 3.1 Changes to uasyncio
120
-
121
- ### 3.1.1 Syntax changes
122
-
123
- * Task cancellation: ` cancel ` is now a method of a ` Task ` instance.
124
- * Event loop methods: ` call_at ` , ` call_later ` , ` call_later_ms ` and
125
- ` call_soon ` are no longer supported. In CPython docs these are
126
- [ lightly deprecated] ( https://docs.python.org/3/library/asyncio-eventloop.html#preface )
127
- in application code; there are simple workrounds.
128
- * ` yield ` in coroutines must be replaced by ` await asyncio.sleep_ms(0) ` :
129
- this is in accord with CPython where ` yield ` will produce a syntax error.
130
- * Awaitable classes. The ` __iter__ ` method works but ` yield ` must be replaced
131
- by ` await asyncio.sleep_ms(0) ` .
132
-
133
- It is possible to write an awaitable class with code portable between
134
- MicroPython and CPython 3.8. This is discussed
135
- [ in the tutorial] ( ./docs/TUTORIAL.md#412-portable-code ) .
136
-
137
- ### 3.1.2 Change to stream I/O
138
-
139
- Classes based on ` uio.IOBase ` will need changes to the ` write ` method. See
140
- [ tutorial] ( ./docs/TUTORIAL.md#64-writing-streaming-device-drivers ) .
141
-
142
- ### 3.1.3 Early task creation
143
-
144
- It is [ bad practice] ( https://github.com/micropython/micropython/issues/6174 )
145
- to create tasks before issuing ` asyncio.run() ` . CPython 3.8 throws if you do.
146
- Such code can be ported by wrapping functions that create tasks in a
147
- coroutine as below.
148
-
149
- There is a subtlety affecting code that creates tasks early:
150
- ` loop.run_forever() ` did just that, never returning and scheduling all created
151
- tasks. By contrast ` asyncio.run(coro()) ` terminates when the coro does. Typical
152
- firmware applications run forever so the coroutine started by ` .run() ` must
153
- ` await ` a continuously running task. This may imply exposing an asynchronous
154
- method which runs forever:
155
-
156
- ``` python
157
- async def main ():
158
- obj = MyObject() # Constructor creates tasks
159
- await obj.run_forever() # Never terminates
160
-
161
- def run (): # Entry point
162
- try :
163
- asyncio.run(main())
164
- finally :
165
- asyncio.new_event_loop()
166
- ```
167
-
168
- ## 3.2 Modules from this repository
169
-
170
- Modules ` asyn.py ` and ` aswitch.py ` are deprecated for V3 applications. See
171
- [ the tutorial] ( ./docs/TUTORIAL.md#3-synchronisation ) for V3 replacements which
172
- are more RAM-efficient.
173
-
174
- ### 3.2.1 Synchronisation primitives
175
-
176
- These were formerly provided in ` asyn.py ` and may now be found in the
177
- ` primitives ` directory, along with additional unofficial primitives.
178
-
179
- The CPython ` asyncio ` library supports these synchronisation primitives:
180
- * ` Lock ` - already incorporated in new ` uasyncio ` .
181
- * ` Event ` - already incorporated.
182
- * ` gather ` - already incorporated.
183
- * ` Semaphore ` and ` BoundedSemaphore ` . In this repository.
184
- * ` Condition ` . In this repository.
185
- * ` Queue ` . In this repository.
186
-
187
- The above unofficial primitives are CPython compatible. Using future official
188
- versions will require a change to the import statement only.
189
-
190
- ### 3.2.2 Synchronisation primitives (old asyn.py)
191
-
192
- Applications using ` asyn.py ` should no longer import that module. Equivalent
193
- functionality may now be found in the ` primitives ` directory: this is
194
- implemented as a Python package enabling RAM savings. The new versions are also
195
- more efficient, replacing polling with the new ` Event ` class.
196
-
197
- These features in ` asyn.py ` were workrounds for bugs in V2 and should not be
198
- used with V3:
199
- * The cancellation decorators and classes (cancellation works as per CPython).
200
- * The nonstandard support for ` gather ` (now properly supported).
201
-
202
- The ` Event ` class in ` asyn.py ` is now replaced by ` Message ` - this is discussed
203
- in [ the tutorial] ( ./docs/TUTORIAL.md#36-message ) .
204
-
205
- ### 3.2.3 Switches, Pushbuttons and delays (old aswitch.py)
206
-
207
- Applications using ` aswitch.py ` should no longer import that module. Equivalent
208
- functionality may now be found in the ` primitives ` directory: this is
209
- implemented as a Python package enabling RAM savings.
210
-
211
- New versions are provided in this repository. Classes:
212
- * ` Delay_ms ` Software retriggerable monostable (watchdog-like object).
213
- * ` Switch ` Debounced switch with close and open callbacks.
214
- * ` Pushbutton ` Pushbutton with double-click and long press callbacks.
0 commit comments