gevent-协程用法

文章介绍了一种采用循环的方式生产协程列表,并可以向协程函数传递参数。。。

# 协程引用
import gevent
from gevent import monkey, pool
monkey.patch_all()
# 初始化协程池 
poolNum = 10
pool = pool.Pool(poolNum)      # 实例化一个协程池
coroutine_list = []
# 构造协程列表 
coroutine = [pool.spawn(coroutineFunc, param=i) for i in list]
# 等待协程执行完毕
gevent.joinall(coroutine)
# 协程列表释放
del coroutine

# 协程执行函数
def coroutineFunc(param):
  pass

注意:协程列表使用完毕后一定要主动释放,列表不会自动释放。

猜你喜欢

转载自www.cnblogs.com/GavinSimons/p/9988218.html