2019-11-13 05:08:22 -05:00
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
import asyncio
|
2019-11-13 05:08:22 -05:00
|
|
|
except ImportError:
|
2023-08-22 11:15:46 -04:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2019-11-13 05:08:22 -05:00
|
|
|
|
2023-10-18 17:49:14 -04:00
|
|
|
# CIRCUITPY provides __await()__
|
2023-08-20 09:02:31 -04:00
|
|
|
async def foo():
|
|
|
|
return 42
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
foo().__await__
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2022-08-18 02:57:45 -04:00
|
|
|
import time
|
2019-11-13 05:08:22 -05:00
|
|
|
|
2022-08-18 02:57:45 -04:00
|
|
|
if hasattr(time, "ticks_ms"):
|
2023-08-22 11:15:46 -04:00
|
|
|
ticks = time.ticks_ms
|
|
|
|
ticks_diff = time.ticks_diff
|
2022-08-18 02:57:45 -04:00
|
|
|
else:
|
2019-11-13 05:08:22 -05:00
|
|
|
ticks = lambda: int(time.time() * 1000)
|
|
|
|
ticks_diff = lambda t1, t0: t1 - t0
|
|
|
|
|
|
|
|
|
|
|
|
async def delay_print(t, s):
|
|
|
|
await asyncio.sleep(t)
|
|
|
|
print(s)
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
print("start")
|
|
|
|
|
|
|
|
await asyncio.sleep(0.001)
|
|
|
|
print("after sleep")
|
|
|
|
|
|
|
|
t0 = ticks()
|
2022-05-03 08:39:58 -04:00
|
|
|
await delay_print(0.2, "short")
|
2019-11-13 05:08:22 -05:00
|
|
|
t1 = ticks()
|
2022-05-03 08:39:58 -04:00
|
|
|
await delay_print(0.4, "long")
|
2019-11-13 05:08:22 -05:00
|
|
|
t2 = ticks()
|
2020-08-20 09:11:44 -04:00
|
|
|
await delay_print(-1, "negative")
|
|
|
|
t3 = ticks()
|
|
|
|
|
|
|
|
print(
|
|
|
|
"took {} {} {}".format(
|
2022-05-03 08:39:58 -04:00
|
|
|
round(ticks_diff(t1, t0), -2),
|
|
|
|
round(ticks_diff(t2, t1), -2),
|
|
|
|
round(ticks_diff(t3, t2), -2),
|
2020-08-20 09:11:44 -04:00
|
|
|
)
|
|
|
|
)
|
2019-11-13 05:08:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
asyncio.run(main())
|