@@ -125,11 +125,11 @@ and rebuilding.
125
125
126
126
5.2 [ Polling hardware with a coroutine] ( ./TUTORIAL.md#52-polling-hardware-with-a-coroutine )
127
127
128
- 5.3 [ Using the IORead mechnanism] ( ./TUTORIAL.md#53-using-the-ioread -mechanism )
128
+ 5.3 [ Using the stream mechnanism] ( ./TUTORIAL.md#53-using-the-stream -mechanism )
129
129
130
130
5.3.1 [ A UART driver example] ( ./TUTORIAL.md#531-a-uart-driver-example )
131
131
132
- 5.4 [ Writing IORead device drivers] ( ./TUTORIAL.md#54-writing-ioread -device-drivers )
132
+ 5.4 [ Writing streaming device drivers] ( ./TUTORIAL.md#54-writing-streaming -device-drivers )
133
133
134
134
5.5 [ A complete example: aremote.py] ( ./TUTORIAL.md#55-a-complete-example-aremotepy )
135
135
A driver for an IR remote control receiver.
@@ -186,8 +186,8 @@ The following modules are provided which may be copied to the target hardware.
186
186
** Libraries**
187
187
188
188
1 . ` asyn.py ` Provides synchronisation primitives ` Lock ` , ` Event ` , ` Barrier ` ,
189
- ` Semaphore ` and ` BoundedSemaphore ` . Provides support for task cancellation via
190
- ` NamedTask ` and ` Cancellable ` classes.
189
+ ` Semaphore ` , ` BoundedSemaphore ` , ` Condition ` and ` gather ` . Provides support
190
+ for task cancellation via ` NamedTask ` and ` Cancellable ` classes.
191
191
2 . ` aswitch.py ` This provides classes for interfacing switches and
192
192
pushbuttons and also a software retriggerable delay object. Pushbuttons are a
193
193
generalisation of switches providing logical rather than physical status along
@@ -215,7 +215,7 @@ results by accessing Pyboard hardware.
215
215
11 . ` auart_hd.py ` Use of the Pyboard UART to communicate with a device using a
216
216
half-duplex protocol. Suits devices such as those using the 'AT' modem command
217
217
set.
218
- 12 . ` iorw.py ` Demo of a read/write device driver using the IORead mechanism.
218
+ 12 . ` iorw.py ` Demo of a read/write device driver using the stream I/O mechanism.
219
219
220
220
** Test Programs**
221
221
@@ -247,14 +247,14 @@ Consider the following example:
247
247
248
248
``` python
249
249
import uasyncio as asyncio
250
- loop = asyncio.get_event_loop()
251
250
async def bar ():
252
251
count = 0
253
252
while True :
254
253
count += 1
255
254
print (count)
256
255
await asyncio.sleep(1 ) # Pause 1s
257
256
257
+ loop = asyncio.get_event_loop()
258
258
loop.create_task(bar()) # Schedule ASAP
259
259
loop.run_forever()
260
260
```
@@ -272,11 +272,12 @@ loop's `run_until_complete` method. Examples of this may be found in the
272
272
` astests.py ` module.
273
273
274
274
The event loop instance is a singleton, instantiated by a program's first call
275
- to ` asyncio.get_event_loop() ` . This takes an optional integer arg being the
276
- length of the coro queue - i.e. the maximum number of concurrent coros allowed.
277
- The default of 42 is likely to be adequate for most purposes. If a coro needs
278
- to call an event loop method, calling ` asyncio.get_event_loop() ` (without
279
- args) will efficiently return it.
275
+ to ` asyncio.get_event_loop() ` . This takes two optional integer args being the
276
+ lengths of the two coro queues - i.e. the maximum number of concurrent coros
277
+ allowed. The default of 16 is likely to be adequate for most purposes.
278
+
279
+ If a coro needs to call an event loop method (usually ` create_task ` ), calling
280
+ ` asyncio.get_event_loop() ` (without args) will efficiently return it.
280
281
281
282
###### [ Contents] ( ./TUTORIAL.md#contents )
282
283
@@ -317,7 +318,8 @@ the `roundrobin.py` example.
317
318
any required arguments passed. The ` run_until_complete ` call returns when
318
319
the coro terminates: this method provides a way of quitting the scheduler.
319
320
* ` await ` Arg: the coro to run, specified with function call syntax. Starts
320
- the coro ASAP and blocks until it has run to completion.
321
+ the coro ASAP. The awaiting coro blocks until the awaited one has run to
322
+ completion.
321
323
322
324
The above are compatible with CPython. Additional uasyncio methods are
323
325
discussed in 2.2.3 below.
@@ -395,14 +397,15 @@ compete to access a single resource. An example is provided in the `astests.py`
395
397
program and discussed in [ the docs] ( ./DRIVERS.md ) . Another hazard is the "deadly
396
398
embrace" where two coros each wait on the other's completion.
397
399
398
- In simple applications communication may be achieved with global flags. A more
399
- elegant approach is to use synchronisation primitives. The module
400
+ In simple applications communication may be achieved with global flags or bound
401
+ variables. A more elegant approach is to use synchronisation primitives. The
402
+ module
400
403
[ asyn.py] ( https://github.com/peterhinch/micropython-async/blob/master/asyn.py )
401
404
offers "micro" implementations of ` Event ` , ` Barrier ` , ` Semaphore ` and
402
405
` Condition ` primitives. These are for use only with asyncio. They are not
403
406
thread safe and should not be used with the ` _thread ` module or from an
404
407
interrupt handler except where mentioned. A ` Lock ` primitive is provided which
405
- is partially superseded by an official implementation.
408
+ is an alterantive to the official implementation.
406
409
407
410
Another synchronisation issue arises with producer and consumer coros. The
408
411
producer generates data which the consumer uses. Asyncio provides the ` Queue `
@@ -460,9 +463,9 @@ ineffective. It will not receive the `TimeoutError` until it has acquired the
460
463
lock. The same observation applies to task cancellation.
461
464
462
465
The module ` asyn.py ` offers a ` Lock ` class which works in these situations
463
- [ full details ] ( ./PRIMITIVES.md#32-class-lock ) . It is significantly less
464
- efficient than the official class but supports additional interfaces as per the
465
- CPython version including context manager usage.
466
+ [ see docs ] ( ./PRIMITIVES.md#32-class-lock ) . It is significantly less efficient
467
+ than the official class but supports additional interfaces as per the CPython
468
+ version including context manager usage.
466
469
467
470
###### [ Contents] ( ./TUTORIAL.md#contents )
468
471
@@ -691,9 +694,9 @@ async def foo():
691
694
print (' Done' )
692
695
```
693
696
694
- ** Note** It is bad practice to issue the close() method to a de-scheduled
695
- coro. This subverts the scheduler by causing the coro to execute code even
696
- though descheduled. This is likely to have unwanted consequences.
697
+ ** Note** It is bad practice to issue the ` close ` or ` throw ` methods of a
698
+ de-scheduled coro. This subverts the scheduler by causing the coro to execute
699
+ code even though descheduled. This is likely to have unwanted consequences.
697
700
698
701
###### [ Contents] ( ./TUTORIAL.md#contents )
699
702
@@ -1015,7 +1018,7 @@ while a coroutine awaiting the outcome polls the object each time it is
1015
1018
scheduled.
1016
1019
1017
1020
Polling may be effected in two ways, explicitly or implicitly. The latter is
1018
- performed by using the ` IORead ` mechanism which is a system designed for stream
1021
+ performed by using the ` stream I/O ` mechanism which is a system designed for stream
1019
1022
devices such as UARTs and sockets. At its simplest explicit polling may consist
1020
1023
of code like this:
1021
1024
@@ -1034,10 +1037,10 @@ an awaitable class might be used. Explicit polling is discussed
1034
1037
further [ below] ( ./TUTORIAL.md#52-polling-hardware-with-a-coroutine ) .
1035
1038
1036
1039
Implicit polling consists of designing the driver to behave like a stream I/O
1037
- device such as a socket or UART, using ` IORead ` . This polls devices using
1040
+ device such as a socket or UART, using ` stream I/O ` . This polls devices using
1038
1041
Python's ` select.poll ` system: because the polling is done in C it is faster
1039
- and more efficient than explicit polling. The use of ` IORead ` is discussed
1040
- [ here] ( ./TUTORIAL.md#53-using-the-ioread -mechanism ) .
1042
+ and more efficient than explicit polling. The use of ` stream I/O ` is discussed
1043
+ [ here] ( ./TUTORIAL.md#53-using-the-stream -mechanism ) .
1041
1044
1042
1045
###### [ Contents] ( ./TUTORIAL.md#contents )
1043
1046
@@ -1099,7 +1102,7 @@ driver implements a `RecordOrientedUart` class, where data is supplied in
1099
1102
variable length records consisting of bytes instances. The object appends a
1100
1103
delimiter before sending and buffers incoming data until the delimiter is
1101
1104
received. This is a demo and is an inefficient way to use a UART compared to
1102
- IORead .
1105
+ stream I/O .
1103
1106
1104
1107
For the purpose of demonstrating asynchronous transmission we assume the
1105
1108
device being emulated has a means of checking that transmission is complete
@@ -1159,7 +1162,7 @@ loop.run_until_complete(run())
1159
1162
1160
1163
###### [ Contents] ( ./TUTORIAL.md#contents )
1161
1164
1162
- ## 5.3 Using the IORead Mechanism
1165
+ ## 5.3 Using the stream mechanism
1163
1166
1164
1167
This can be illustrated using a Pyboard UART. The following code sample
1165
1168
demonstrates concurrent I/O on one UART. To run, link Pyboard pins X1 and X2
@@ -1191,10 +1194,10 @@ loop.run_forever()
1191
1194
The supporting code may be found in ` __init__.py ` in the ` uasyncio ` library.
1192
1195
The mechanism works because the device driver (written in C) implements the
1193
1196
following methods: ` ioctl ` , ` read ` , ` readline ` and ` write ` . See
1194
- [ Writing IORead device drivers] ( ./TUTORIAL.md#54-writing-ioread -device-drivers )
1197
+ [ Writing streaming device drivers] ( ./TUTORIAL.md#54-writing-streaming -device-drivers )
1195
1198
for details on how such drivers may be written in Python.
1196
1199
1197
- A UART can receive data at any time. The IORead mechanism checks for pending
1200
+ A UART can receive data at any time. The stream I/O mechanism checks for pending
1198
1201
incoming characters whenever the scheduler has control. When a coro is running
1199
1202
an interrupt service routine buffers incoming characters; these will be removed
1200
1203
when the coro yields to the scheduler. Consequently UART applications should be
@@ -1227,9 +1230,9 @@ returned. See the code comments for more details.
1227
1230
1228
1231
###### [ Contents] ( ./TUTORIAL.md#contents )
1229
1232
1230
- ## 5.4 Writing IORead device drivers
1233
+ ## 5.4 Writing streaming device drivers
1231
1234
1232
- The ` IORead ` mechanism is provided to support I/O to stream devices. Its
1235
+ The ` stream I/O ` mechanism is provided to support I/O to stream devices. Its
1233
1236
typical use is to support streaming I/O devices such as UARTs and sockets. The
1234
1237
mechanism may be employed by drivers of any device which needs to be polled:
1235
1238
the polling is delegated to the scheduler which uses ` select ` to schedule the
@@ -1238,7 +1241,7 @@ multiple coros each polling a device, partly because `select` is written in C
1238
1241
but also because the coroutine performing the polling is descheduled until the
1239
1242
` poll ` object returns a ready status.
1240
1243
1241
- A device driver capable of employing the IORead mechanism may support
1244
+ A device driver capable of employing the stream I/O mechanism may support
1242
1245
` StreamReader ` , ` StreamWriter ` instances or both. A readable device must
1243
1246
provide at least one of the following methods. Note that these are synchronous
1244
1247
methods. The ` ioctl ` method (see below) ensures that they are only called if
0 commit comments