博客轉載html
http://www.cnblogs.com/alex3714/articles/5248247.htmlide
http://www.cnblogs.com/tkqasn/p/5705338.html函數
代碼示例spa
import greenlet def func1(): print(1) gr2.switch() print(1) gr2.switch() def func2(): print(2) gr3.switch() print(2) gr3.switch() def func3(): print(3) gr1.switch() print(3) gr1.switch() # greenlet能夠實現函數間的任意中斷切換執行 gr1 = greenlet.greenlet(func1) gr2 = greenlet.greenlet(func2) gr3 = greenlet.greenlet(func3) gr1.switch()
import gevent def func1(): print(1) gevent.sleep() print(1) def func2(): print(2) gevent.sleep(1) print(2) def func3(): print(3) gevent.sleep() print(3) # gevent封裝了greenlet,能夠經過sleep()交出控制權,實現協程 gevent.joinall([ gevent.spawn(func1), gevent.spawn(func2), gevent.spawn(func3) ])