Skip to content

Commit a4c96fb

Browse files
committed
extmod/uasyncio: Add asyncio.wait_for_ms function.
Fixes issue adafruit#6107.
1 parent f3062b5 commit a4c96fb

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

docs/library/uasyncio.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ Additional functions
7474

7575
This is a coroutine.
7676

77+
.. function:: wait_for_ms(awaitable, timeout)
78+
79+
Similar to `wait_for` but *timeout* is an integer in milliseconds.
80+
81+
This is a coroutine, and a MicroPython extension.
82+
7783
.. function:: gather(\*awaitables, return_exceptions=False)
7884

7985
Run all *awaitables* concurrently. Any *awaitables* that are not tasks are

extmod/uasyncio/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
_attrs = {
99
"wait_for": "funcs",
10+
"wait_for_ms": "funcs",
1011
"gather": "funcs",
1112
"Event": "event",
1213
"Lock": "lock",

extmod/uasyncio/funcs.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
from . import core
55

66

7-
async def wait_for(aw, timeout):
7+
async def wait_for(aw, timeout, sleep=core.sleep):
88
aw = core._promote_to_task(aw)
99
if timeout is None:
1010
return await aw
1111

12-
def cancel(aw, timeout):
13-
await core.sleep(timeout)
12+
def cancel(aw, timeout, sleep):
13+
await sleep(timeout)
1414
aw.cancel()
1515

16-
cancel_task = core.create_task(cancel(aw, timeout))
16+
cancel_task = core.create_task(cancel(aw, timeout, sleep))
1717
try:
1818
ret = await aw
1919
except core.CancelledError:
@@ -29,6 +29,10 @@ def cancel(aw, timeout):
2929
return ret
3030

3131

32+
def wait_for_ms(aw, timeout):
33+
return wait_for(aw, timeout, core.sleep_ms)
34+
35+
3236
async def gather(*aws, return_exceptions=False):
3337
ts = [core._promote_to_task(aw) for aw in aws]
3438
for i in range(len(ts)):
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Test MicroPython extensions on CPython asyncio:
2+
# - sleep_ms
3+
# - wait_for_ms
4+
5+
try:
6+
import utime, uasyncio
7+
except ImportError:
8+
print("SKIP")
9+
raise SystemExit
10+
11+
12+
async def task(id, t):
13+
print("task start", id)
14+
await uasyncio.sleep_ms(t)
15+
print("task end", id)
16+
return id * 2
17+
18+
19+
async def main():
20+
# Simple sleep_ms
21+
t0 = utime.ticks_ms()
22+
await uasyncio.sleep_ms(1)
23+
print(utime.ticks_diff(utime.ticks_ms(), t0) < 100)
24+
25+
# When task finished before the timeout
26+
print(await uasyncio.wait_for_ms(task(1, 5), 50))
27+
28+
# When timeout passes and task is cancelled
29+
try:
30+
print(await uasyncio.wait_for_ms(task(2, 50), 5))
31+
except uasyncio.TimeoutError:
32+
print("timeout")
33+
34+
print("finish")
35+
36+
37+
uasyncio.run(main())
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
True
2+
task start 1
3+
task end 1
4+
2
5+
task start 2
6+
timeout
7+
finish

0 commit comments

Comments
 (0)