@@ -52,6 +52,7 @@ asyncio and includes a section for complete beginners.
52
52
6.4 [ Testing] ( ./TUTORIAL.md#64-testing )
53
53
6.5 [ A common error] ( ./TUTORIAL.md#65-a-common-error ) This can be hard to find.
54
54
6.6 [ Socket programming] ( ./TUTORIAL.md#66-socket-programming )
55
+ 6.7 [ Event loop constructor args] ( ./TUTORIAL.md#67-event-loop-constructor-args )
55
56
7 . [ Notes for beginners] ( ./TUTORIAL.md#7-notes-for-beginners )
56
57
7.1 [ Problem 1: event loops] ( ./TUTORIAL.md#71-problem-1:-event-loops )
57
58
7.2 [ Problem 2: blocking methods] ( ./TUTORIAL.md#7-problem-2:-blocking-methods )
@@ -240,7 +241,8 @@ The event loop instance is a singleton, instantiated by a program's first call
240
241
to ` asyncio.get_event_loop() ` . This takes two optional integer args being the
241
242
lengths of the two coro queues. Typically both will have the same value being
242
243
at least the number of concurrent coros in the application. The default of 16
243
- is usually sufficient.
244
+ is usually sufficient. If using non-default values see
245
+ [ Event loop constructor args] ( ./TUTORIAL.md#67-event-loop-constructor-args ) .
244
246
245
247
If a coro needs to call an event loop method (usually ` create_task ` ), calling
246
248
` asyncio.get_event_loop() ` (without args) will efficiently return it.
@@ -984,9 +986,9 @@ while a coroutine awaiting the outcome polls the object each time it is
984
986
scheduled.
985
987
986
988
Polling may be effected in two ways, explicitly or implicitly. The latter is
987
- performed by using the ` stream I/O ` mechanism which is a system designed for stream
988
- devices such as UARTs and sockets. At its simplest explicit polling may consist
989
- of code like this:
989
+ performed by using the ` stream I/O ` mechanism which is a system designed for
990
+ stream devices such as UARTs and sockets. At its simplest explicit polling may
991
+ consist of code like this:
990
992
991
993
``` python
992
994
async def poll_my_device ():
@@ -1359,11 +1361,11 @@ time of writing there is a bug in `uasyncio` which prevents this from woking.
1359
1361
See [ this GitHub thread] ( https://github.com/micropython/micropython/pull/3836#issuecomment-397317408 ) .
1360
1362
There are two solutions. A workround is to write two separate drivers, one
1361
1363
read-only and the other write-only. Alternatively the
1362
- [ fast_io] ( ./FASTPOLL.md ) addresses this.
1364
+ [ fast_io] ( ./FASTPOLL.md ) version addresses this.
1363
1365
1364
1366
In the official ` uasyncio ` I/O is scheduled quite infrequently. See
1365
1367
[ see this GitHub RFC] ( https://github.com/micropython/micropython/issues/2664 ) .
1366
- The ` fast_io ` version addresses this issue.
1368
+ The [ fast_io] ( ./FASTPOLL.md ) version addresses this issue.
1367
1369
1368
1370
###### [ Contents] ( ./TUTORIAL.md#contents )
1369
1371
@@ -1374,7 +1376,7 @@ The demo provides a complete device driver example: a receiver/decoder for an
1374
1376
infra red remote controller. The following notes are salient points regarding
1375
1377
its ` asyncio ` usage.
1376
1378
1377
- A pin interrupt records the time of a state change (in us ) and sets an event,
1379
+ A pin interrupt records the time of a state change (in μs ) and sets an event,
1378
1380
passing the time when the first state change occurred. A coro waits on the
1379
1381
event, yields for the duration of a data burst, then decodes the stored data
1380
1382
before calling a user-specified callback.
@@ -1552,6 +1554,35 @@ An alternative approach is to use blocking sockets with `StreamReader` and
1552
1554
1553
1555
###### [ Contents] ( ./TUTORIAL.md#contents )
1554
1556
1557
+ ## 6.7 Event loop constructor args
1558
+
1559
+ A subtle bug can arise if you need to instantiate the event loop with non
1560
+ default values. Instantiation should be performed before running any other
1561
+ ` asyncio ` code. This is because the code may acquire the event loop. In
1562
+ doing so it initialises it to the default values:
1563
+
1564
+ ``` python
1565
+ import uasyncio as asyncio
1566
+ import some_module
1567
+ bar = some_module.Bar() # Constructor calls get_event_loop()
1568
+ # and renders these args inoperative
1569
+ loop = asyncio.get_event_loop(runq_len = 40 , waitq_len = 40 )
1570
+ ```
1571
+
1572
+ Given that importing a module can run code the only safe way is to instantiate
1573
+ the event loop immediately after importing ` uasyncio ` .
1574
+
1575
+ ``` python
1576
+ import uasyncio as asyncio
1577
+ loop = asyncio.get_event_loop(runq_len = 40 , waitq_len = 40 )
1578
+ import some_module
1579
+ bar = some_module.Bar() # The get_event_loop() call is now safe
1580
+ ```
1581
+
1582
+ Ref [ this issue] ( https://github.com/micropython/micropython-lib/issues/295 ) .
1583
+
1584
+ ###### [ Contents] ( ./TUTORIAL.md#contents )
1585
+
1555
1586
# 7 Notes for beginners
1556
1587
1557
1588
These notes are intended for those new to asynchronous code. They start by
0 commit comments