#greenlet
1 import time
from greenlet import greenlet
# greenlet可以实现一个自行调度的微线程
def work1():
while True:
print("正在执行work1()")
time.sleep(0.5)#模拟阻塞
# 如果遇到阻塞的情况下,切换到第二个任务
g2.switch() def work2():
while True:
print("正在执行work2()")
time.sleep(0.5)
# 切换到第一个任务
g1.switch() if __name__ == '__main__':
# 创建greenlet的对象 greenlet(函数名)
g1 = greenlet(work1)
g2 = greenlet(work2)
g1.switch()