Github - gevent/gevent
Python - grequests并发发送请求库 - AIUAI
grequests 是基于
gevent+requests
的一个并发发送请求的库.
基于 gevent 协程并发示例,
示例1
#!/usr/bin/python3
#!--*-- coding: utf-8 --*--
import time
import requests
import gevent
from gevent.pool import Pool
from gevent import monkey
monkey.patch_all()
def get_ip(i):
#返回当前的ip地址
url = 'https://api.ipify.org/'
print(i, time.strftime('%Y-%m-%d %H:%M:%S'), 'requests')
rs = requests.get(url)
print(i, time.strftime('%Y-%m-%d %H:%M:%S'), rs.content.decode())
if __name__ == "__main__":
p = Pool(99) # 限制协程池里最多同时运行数量
jobs = []
for n in range(6):
jobs.append(p.spawn(get_ip, n)) # 这里添加了6个任务
gevent.joinall(jobs)
print('end')
示例2
#!/usr/bin/python3
#!--*-- coding: utf-8 --*--
import time
import gevent
from gevent.pool import Pool
from gevent import monkey
monkey.patch_all()
class geventModel(object):
def __init__(self):
self.p = Pool(20) #限制最大并发数
self.results = None
self.num = 5
#导入gevent猴子补丁,否则协程不会并发执行
#待并发执行任务
def task(self, i, num):
time.sleep(i)
self.num += 1
self.results = self.num
print("sum = {}".format(self.num))
def run(self):
time_l=time.time()
#任务派发,将15个任务派发给协程去做
threads = [self.p.spawn(self.task, 5, self.num) for i in range(40)]
#在此阻塞,等所有协程全部完成退出,这一步才执行完
gevent.joinall(threads)
time_r=time.time()
print("总耗时:{}".format(time_r-time_l))
if __name__ == "__main__":
gevent_ins = geventModel()
gevent_ins.run()
print(gevent_ins.results)