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......

 

 

posted @ 2023-03-23 17:29  筱筱的春天  阅读(46)  评论(0编辑  收藏  举报