https://github.com/peterhinch/micropython-async
http://docs.micropython.org/en/latest/library/uasyncio.html
CPython 支持通过库进行异步编程。而MicroPython 提供了其中的一个子集,针对裸机目标的小型代码大小和高性能进行了优化。
v2是以前的实现版本
v3是现在在使用的版本
async def blink(led, period_ms):
while True:
led.on()
await uasyncio.sleep_ms(5)
led.off()
await uasyncio.sleep_ms(period_ms)
async def main(led1, led2):
uasyncio.create_task(blink(led1, 700))
uasyncio.create_task(blink(led2, 400))
await uasyncio.sleep_ms(10_000)
# 在pyboard上运行
from pyb import LED
uasyncio.run(main(LED(1), LED(2)))
#在通用板上运行
from machine import Pin
uasyncio.run(main(Pin(1), Pin(2)))
在具体的论述之前,我们分享一段代码先睹为快
看上去和我们主机的py代码一样其实是因为V3重构代码的缘故
https://github.com/micropython/micropython/pull/5332
https://github.com/peterhinch/micropython-async/blob/master/v3/README.md
这篇文章里面讲了从V2转到V3 的一些改变,我觉得国内的人不用改,最多就是看见点个灯就完事了,底层实现又不关心。