已此段代码为例
import eventlet
import time
eventlet.patcher.monkey_patch(all=True)
def p1(a):
for i in range(5):
print "%s..." % a
time.sleep(0.2)
pool = eventlet.GreenPool(size=20)
for a in range(20):
pool.spawn_n(p1, a)
time.sleep(5)
import eventlet:初始化eventlet,生成hubs,但此时hubs仍然在后台,没有执行。
eventlet.patcher.monkey_patch(all=True):将'os', 'select', 'socket','thread', 'time', 'psycopg', 'MySQLdb','builtins'中耗时较长的操作替换为eventlet的函数。
pool.spawn_n(p1, a):本质为一个时间为0的定时器,将函数p1和参数作为调用栈,对应的时间为timer(0)注册到hubs中。
当函数运行到time.sleep(5)之前时,hubs都处于后台未执行状态,由于sleep被替换,新的sleep操作为将目前的执行栈及对应定时器时间注册到hubs中,并switch_to到hubs中。
hubs运行伪代码如下(详细可参考eventlet.hubs.poll.Hub.wait):
while True:
results = doPoll()
callbacks = set()
for func, event in results.items():
if event is "OK":
callbacks.add(func)
for func in callbacks:
func.run()
一旦到hubs中,hubs会不断的轮询队列中的事件,一旦事件就绪就会将其函数栈添加到临时列表中顺序执行。
以p1为例,当执行到time.sleep(5)之前时,hubs中有20个p1函数,其对应的事件为timer(0),调用完sleep(5)之后hubs中为21个事件,每次p1中执行到sleep(0.2)时,就会重新注册事件并switch到hubs中。
当然,以上案例只是为了说明hubs工作原理,因为Hubs优化了定时器,其他io或者网络操作需要调用io复用模型,定时器更简单故而可以省去轮询的过程并且优先级更高使时间更准确,但是整体调度机制类似。