python 协程 第三方greenlet
1. greenlet
自行执行的微线程
https://greenlet.readthedocs.io/en/latest/greenlet.html
2. 终端安装
pip install greenlet
3. 举个栗子
import time
from greenlet import greenlet
def work1():
while True:
print("executing work1......")
time.sleep(0.5)
g2.switch()
def work2():
while True:
print("executing work2......")
time.sleep(0.5)
g1.switch()
if __name__ == '__main__':
g1 = greenlet(work1)
g2 = greenlet(work2)
g1.switch()
result
executing work1......
executing work2......
executing work1......
executing work2......
executing work1......
executing work2......
executing work1......
executing work2......