收藏

micropython esp32 中的多线程

分类: 基础知识篇 >> Micropython基础知识 发布于 2024-05-16 358次阅读0点赞0收藏

我们可以使用_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)
VIP
购买
建议
意见
联系
客服
在线咨询

您可以与在线客服进行沟通获得帮助

工作日:8:00~22:00节假日:9:00~20:00

微信号: mpyos01

Q Q号: 1401211620

免费
福利
0.029884s