1. 程式人生 > >python 協程之Greenlet

python 協程之Greenlet

test switch bsp () pre nbsp from def port

greenlet是一個用C實現的協程模塊,相比與python自帶的yield,它可以使你在任意函數之間隨意切換,而不需把這個函數先聲明為generator。

from greenlet import greenlet
def test1():
    print(12)
    gr2.switch()
    print(34)
    gr2.switch()
def test2():
    print(56)
    gr1.switch()
    print(78)
gr1=greenlet(test1)
gr2=greenlet(test2)
gr1.switch()            
#切換

python 協程之Greenlet