Python程序中的协程操作-greenlet模块
一、安装模块
安装:pip3 install greenlet
二、greenlet实现状态切换
from greenlet import greenlet def eat(name): print('%s eat 1' %name) g2.switch('randy') print('%s eat 2' %name) g2.switch() def play(name): print('%s play 1' %name) g1.switch() print('%s play 2' %name) g1=greenlet(eat) g2=greenlet(play) g1.switch('randy')#可以在第一次switch时传入参数,以后都不需要
randy eat 1
randy play 1
randy eat 2
randy play 2
三、效率对比
单纯的切换(在没有io的情况下或者没有重复开辟内存空间的操作),反而会降低程序的执行速度。
#顺序执行 import time def f1(): res = 1 for i in range(100000000): res += i def f2(): res = 1 for i in range(100000000): res *= i start = time.time() f1() f2() stop = time.time() print('run time is %s' % (stop - start)) # 10.985628366470337
run time is 13.264543294906616
# 切换 from greenlet import greenlet import time def f1(): res = 1 for i in range(100000000): res += i g2.switch() def f2(): res = 1 for i in range(100000000): res *= i g1.switch() start = time.time() g1 = greenlet(f1) g2 = greenlet(f2) g1.switch() stop = time.time() print('run time is %s' % (stop - start)) # 52.763017892837524
greenlet只是提供了一种比generator更加便捷的切换方式,当切到一个任务执行时如果遇到io,那就原地阻塞,仍然是没有解决遇到IO自动切换来提升效率的问题。
单线程里的这20个任务的代码通常会既有计算操作又有阻塞操作,我们完全可以在执行任务1时遇到阻塞,就利用阻塞的时间去执行任务2...如此,才能提高效率,这就用到了Gevent模块。
来源:https://www.cnblogs.com/randysun/p/12258847.html