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

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

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

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

Micropython ESP频道

micropython 协程调度器demo代码


协程demo

#python 协程demo

import time

taskcount=3

def task1():  
  while True:
    print("-----i am task1-----")
    yield(100)
	
def task2():
  while True:
    print("-----i am task2-----")
    yield(200)

def task3():
  while True:
    print("-----i am task3-----")
    yield(300)

timerlist=[0,0,0]	 #任务时间队列
tasklist=[task1(),task2(),task3()]

def updatetimers():
  for i in range(0,taskcount):
     if timerlist[i]>0:
       timerlist[i]=timerlist[i]-1

def runtasks():	
  for i in range(0,taskcount):   
    if timerlist[i]==0:
      timerlist[i]=next(tasklist[i])

while True:
    time.sleep(0.001)
    updatetimers()
    runtasks()



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

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0098898410797119 seconds