Skip to content

Commit 78bb133

Browse files
committed
Tutorial improvements.
1 parent 3941ce5 commit 78bb133

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

TUTORIAL.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,16 @@ because this has been placed on the scheduler's queue by `loop.create_task`.
266266
In this trivial example there is only one coro: `bar`. If there were others,
267267
the scheduler would schedule them in periods when `bar` was paused.
268268

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
270270
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.
273273

274274
The event loop instance is a singleton, instantiated by a program's first call
275275
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.
278279

279280
If a coro needs to call an event loop method (usually `create_task`), calling
280281
`asyncio.get_event_loop()` (without args) will efficiently return it.
@@ -754,7 +755,8 @@ loop = asyncio.get_event_loop()
754755
loop.run_until_complete(bar())
755756
```
756757

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
758760
`__iter__` must be used. The line `__iter__ = __await__` enables portability
759761
between CPython and MicroPython. Example code may be found in the `Event`,
760762
`Barrier`, `Cancellable` and `Condition` classes in asyn.py.
@@ -912,19 +914,17 @@ value returned by `__aenter__`.
912914

913915
There was a bug in the implementation whereby if an explicit `return` was issued
914916
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).
916918

917919
###### [Contents](./TUTORIAL.md#contents)
918920

919921
## 4.4 Coroutines with timeouts
920922

921-
This requires uasyncio.core V1.7 which was released on 16th Dec 2017, with
922-
firmware of that date or later.
923-
924923
Timeouts are implemented by means of `uasyncio.wait_for()`. This takes as
925924
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.
928928

929929
```python
930930
import uasyncio as asyncio
@@ -946,10 +946,11 @@ loop = asyncio.get_event_loop()
946946
loop.run_until_complete(foo())
947947
```
948948

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.
953954

954955
If this matters to the application, create a long delay by awaiting a short one
955956
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
961962
is not propagated to the scheduler. If this occurred it would stop running,
962963
passing the exception to the code which started the scheduler.
963964

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.
968968

969969
There is a "gotcha" illustrated by this code sample. If allowed to run to
970970
completion it works as expected.

0 commit comments

Comments
 (0)