@@ -266,15 +266,16 @@ because this has been placed on the scheduler's queue by `loop.create_task`.
266
266
In this trivial example there is only one coro: ` bar ` . If there were others,
267
267
the scheduler would schedule them in periods when ` bar ` was paused.
268
268
269
- Many embedded applications have an event loop which runs continuously. The event
269
+ Most embedded applications have an event loop which runs continuously. The event
270
270
loop can also be started in a way which permits termination, by using the event
271
- loop's ` run_until_complete ` method. Examples of this may be found in the
272
- ` astests.py ` module.
271
+ loop's ` run_until_complete ` method; this is mainly of use in testing. Examples
272
+ may be found in the ` astests.py ` module.
273
273
274
274
The event loop instance is a singleton, instantiated by a program's first call
275
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.
276
+ lengths of the two coro queues. Typically both will have the same value being
277
+ at least the number of concurrent coros in the application. The default of 16
278
+ is usually sufficient.
278
279
279
280
If a coro needs to call an event loop method (usually ` create_task ` ), calling
280
281
` asyncio.get_event_loop() ` (without args) will efficiently return it.
@@ -754,7 +755,8 @@ loop = asyncio.get_event_loop()
754
755
loop.run_until_complete(bar())
755
756
```
756
757
757
- Currently MicroPython doesn't support ` __await__ ` (issue #2678 ) and
758
+ Currently MicroPython doesn't support ` __await__ `
759
+ [ issue #2678 ] ( https://github.com/micropython/micropython/issues/2678 ) and
758
760
` __iter__ ` must be used. The line ` __iter__ = __await__ ` enables portability
759
761
between CPython and MicroPython. Example code may be found in the ` Event ` ,
760
762
` Barrier ` , ` Cancellable ` and ` Condition ` classes in asyn.py.
@@ -912,19 +914,17 @@ value returned by `__aenter__`.
912
914
913
915
There was a bug in the implementation whereby if an explicit ` return ` was issued
914
916
within an ` async with ` block, the ` __aexit__ ` method was not called. This was
915
- fixed as of 27th June 2018 [ ref ] ( https://github.com/micropython/micropython/pull/3890 ) .
917
+ fixed as of 27th June 2018 [ PR 3890 ] ( https://github.com/micropython/micropython/pull/3890 ) .
916
918
917
919
###### [ Contents] ( ./TUTORIAL.md#contents )
918
920
919
921
## 4.4 Coroutines with timeouts
920
922
921
- This requires uasyncio.core V1.7 which was released on 16th Dec 2017, with
922
- firmware of that date or later.
923
-
924
923
Timeouts are implemented by means of ` uasyncio.wait_for() ` . This takes as
925
924
arguments a coroutine and a timeout in seconds. If the timeout expires a
926
- ` TimeoutError ` will be thrown to the coro. The next time the coro is scheduled
927
- for execution the exception will be raised: the coro should trap this and quit.
925
+ ` TimeoutError ` will be thrown to the coro in such a way that the next time the
926
+ coro is scheduled for execution the exception will be raised. The coro should
927
+ trap this and quit.
928
928
929
929
``` python
930
930
import uasyncio as asyncio
@@ -946,10 +946,11 @@ loop = asyncio.get_event_loop()
946
946
loop.run_until_complete(foo())
947
947
```
948
948
949
- Note that if the coro awaits a long delay, it will not be rescheduled until the
950
- time has elapsed. The ` TimeoutError ` will occur as soon as the coro is
951
- scheduled. But in real time and from the point of view of the calling coro, its
952
- response to the ` TimeoutError ` will correspondingly be delayed.
949
+ Note that if the coro issues ` await asyncio.sleep(t) ` where ` t ` is a long delay
950
+ it will not be rescheduled until ` t ` has elapsed. If the timeout has elapsed
951
+ before the ` sleep ` is complete the ` TimeoutError ` will occur when the coro is
952
+ scheduled - i.e. when ` t ` has elapsed. In real time and from the point of view
953
+ of the calling coro, its response to the ` TimeoutError ` will be delayed.
953
954
954
955
If this matters to the application, create a long delay by awaiting a short one
955
956
in a loop. The coro ` asyn.sleep ` [ supports this] ( ./PRIMITIVES.md#41-coro-sleep ) .
@@ -961,10 +962,9 @@ or in a coro which is awaiting its completion. This ensures that the exception
961
962
is not propagated to the scheduler. If this occurred it would stop running,
962
963
passing the exception to the code which started the scheduler.
963
964
964
- Using ` throw ` to throw an exception to a coro is unwise. It subverts the design
965
- of ` uasyncio ` by forcing the coro to run, and possibly terminate, when it is
966
- still queued for execution. I haven't entirely thought through the implications
967
- of this, but it's a thoroughly bad idea.
965
+ Using ` throw ` or ` close ` to throw an exception to a coro is unwise. It subverts
966
+ ` uasyncio ` by forcing the coro to run, and possibly terminate, when it is still
967
+ queued for execution.
968
968
969
969
There is a "gotcha" illustrated by this code sample. If allowed to run to
970
970
completion it works as expected.
0 commit comments