Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython esp32 中的多线程


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)

运行结果:

f8b91802978c4e849680d5450579a5da.png



六、建议

在ESP开发板中,如果不是必须不建议使用多线程,因为我们的开关板存储和执行性能有限,多线程会带来大的开销,所以开发过程中我们要合理的安排。

————————————————

原文链接:https://blog.csdn.net/zhusongziye/article/details/128063353



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0091750621795654 seconds