本帖最后由 dcexpert 于 2017-7-9 11:47 编辑
]MicroPython的Pyboard版本已经开始支持线程了,因此做了简单的测试。先下载并更新支持线程功能的固件,然后使用下面代码就可以测试线程了。
- import _thread
- def led(id, dt):
- cnt = 0
- while 1:
- print(id, cnt)
- cnt = cnt + 1
- pyb.LED(id).toggle()
- pyb.delay(dt)
- _thread.start_new_thread(led, (1, 1000))
- _thread.start_new_thread(led, (2, 1500))
复制代码
线程有点类似RTOS中的任务,在函数中定义了一个不退出的循环,通过延时切换任务。目前_thread模块支持的功能有:
>>> dir(_thread)
['__name__', 'LockType', 'get_ident', 'stack_size', 'start_new_thread', 'exit', 'allocate_lock']
此内容由EEWORLD论坛网友dcexpert原创,如需转载或用于商业用途需征得作者同意并注明出处