You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
772 B
37 lines
772 B
# Test MicroPython extensions on CPython asyncio:
|
|
# - sleep_ms
|
|
# - wait_for_ms
|
|
|
|
try:
|
|
import utime, uasyncio
|
|
except ImportError:
|
|
print("SKIP")
|
|
raise SystemExit
|
|
|
|
|
|
async def task(id, t):
|
|
print("task start", id)
|
|
await uasyncio.sleep_ms(t)
|
|
print("task end", id)
|
|
return id * 2
|
|
|
|
|
|
async def main():
|
|
# Simple sleep_ms
|
|
t0 = utime.ticks_ms()
|
|
await uasyncio.sleep_ms(1)
|
|
print(utime.ticks_diff(utime.ticks_ms(), t0) < 100)
|
|
|
|
# When task finished before the timeout
|
|
print(await uasyncio.wait_for_ms(task(1, 5), 50))
|
|
|
|
# When timeout passes and task is cancelled
|
|
try:
|
|
print(await uasyncio.wait_for_ms(task(2, 50), 5))
|
|
except uasyncio.TimeoutError:
|
|
print("timeout")
|
|
|
|
print("finish")
|
|
|
|
|
|
uasyncio.run(main())
|
|
|