本站改版新增arduino频道
micropython esp32 中的多线程
我们可以使用_thread来在ESP32中开发多进程的代码。如下:
import _thread
import time
import sys
import machine
 
 
# ---------- 这是一个线程要执行的代码 ------------
def test1(*args, **kwargs):
    while True:
        print("1")
        time.sleep(1)
 
 
# ---------- 这是另一个线程要执行的代码 ------------
def test2(*args, **kwargs):
    while True:
        print("2")
        time.sleep(1)
 
 
# ---------- 这里创建线程 ------------
thread_1 = _thread.start_new_thread(test1, (1,))
thread_2 = _thread.start_new_thread(test2, (2,))
 
 
# ---------- 这是主线程要执行的代码 ------------
while True:
    print("3")
    time.sleep(1)运行结果:

六、建议
在ESP开发板中,如果不是必须不建议使用多线程,因为我们的开关板存储和执行性能有限,多线程会带来大的开销,所以开发过程中我们要合理的安排。
————————————————
原文链接:https://blog.csdn.net/zhusongziye/article/details/128063353
Copyright © 2014 ESP56.com All Rights Reserved  
晋ICP备14006235号-22 晋公网安备14108102001165号    
执行时间: 0.0093281269073486 seconds