#greenlet
1 import time 2 from greenlet import greenlet 3 # greenlet可以实现一个自行调度的微线程 4 def work1(): 5 while True: 6 print("正在执行work1()") 7 time.sleep(0.5)#模拟阻塞 8 # 如果遇到阻塞的情况下,切换到第二个任务 9 g2.switch() 10 11 def work2(): 12 while True: 13 print("正在执行work2()") 14 time.sleep(0.5) 15 # 切换到第一个任务 16 g1.switch() 17 18 if __name__ == '__main__': 19 # 创建greenlet的对象 greenlet(函数名) 20 g1 = greenlet(work1) 21 g2 = greenlet(work2) 22 g1.switch()