python3-协程的使用(一)
1:requests模块和gevent混合使用时需要先导入gevent再导入requests模块
import gevent
from gevent import monkey
gevent.monkey.patch_all()
2:协程池的使用方法:
from gevent import pool as g_pool
pool = g_pool.Pool(n) # n:线程池中的个数
for tak in task_loaders:
pool.add(gevent.spawn(函数名, 参数))
pool.join()
3:对线程池限定每次运行的个数
from gevent import lock
from gevent import pool as g_pool
pool = g_pool.Pool(n) # n:线程池中的个数
sem = lock.BoundedSemaphore(n) # 限定的个数
for tak in task_loaders:
pool.add(gevent.spawn(func函数名1, 参数))
pool.join()
sem.acquire() # 封死
func函数名1
sem.release() # n个运行之后打开
例如:
sem = lock.BoundedSemaphore(self.thr_nums)
pool = g_pool.Pool(self.thr_nums)
for db_info in curr_tg_infos:
self.sync_db_file_name = db_info[0] + '_入库图片请求响应时间与信息日志'
for image_file in self.image_list:
# self.do_sync_img_step(sem, image_file, db_info)
pool.add(gevent.spawn(self.do_sync_img_step, sem, image_file, db_info))
pool.join()
del (sem)
del (pool)
4:多携程在写数据时防止数据写乱,使用加锁的方式
from gevent import lock
ilock = Lock.RLock()
ilock.acquire()
func(写内容)
ilock.release()