Skip to content

Commit 26b66e3

Browse files
committed
Tutorial: improve timeout section.
1 parent ad181df commit 26b66e3

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

TUTORIAL.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,9 @@ Timeouts are implemented by means of `uasyncio.wait_for()`. This takes as
904904
arguments a coroutine and a timeout in seconds. If the timeout expires a
905905
`TimeoutError` will be thrown to the coro in such a way that the next time the
906906
coro is scheduled for execution the exception will be raised. The coro should
907-
trap this and quit.
907+
trap this and quit; alternatively the calling coro should trap and ignore the
908+
exception. If the exception is not trapped the code will hang: this appears to
909+
be a bug in `uasyncio` V2.0.
908910

909911
```python
910912
import uasyncio as asyncio
@@ -922,6 +924,27 @@ async def foo():
922924
await asyncio.wait_for(forever(), 5)
923925
await asyncio.sleep(2)
924926

927+
loop = asyncio.get_event_loop()
928+
loop.run_until_complete(foo())
929+
```
930+
Alternatively:
931+
```python
932+
import uasyncio as asyncio
933+
934+
async def forever():
935+
print('Starting')
936+
while True:
937+
await asyncio.sleep_ms(300)
938+
print('Got here')
939+
940+
async def foo():
941+
try:
942+
await asyncio.wait_for(forever(), 5)
943+
except asyncio.TimeoutError:
944+
pass
945+
print('Timeout elapsed.')
946+
await asyncio.sleep(2)
947+
925948
loop = asyncio.get_event_loop()
926949
loop.run_until_complete(foo())
927950
```

aswitch.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ def __call__(self):
115115
return self.switchstate
116116

117117
async def switchcheck(self):
118-
loop = asyncio.get_event_loop()
119118
while True:
120119
state = self.pin.value()
121120
if state != self.switchstate:

0 commit comments

Comments
 (0)