温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在Python中利用grequests实现一个并发请求功能

发布时间:2020-11-06 14:30:52 来源:亿速云 阅读:386 作者:Leah 栏目:开发技术

在Python中利用grequests实现一个并发请求功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

前言

requests是Python发送接口请求非常好用的一个三方库,由K神编写,简单,方便上手快。但是requests发送请求是串行的,即阻塞的。发送完一条请求才能发送另一条请求。
为了提升测试效率,一般我们需要并行发送请求。这里可以使用多线程,或者协程,gevent或者aiohttp,然而使用起来,都相对麻烦。

grequests简单使用

首先构造一个请求列表,使用grequests.map()并行发送,得到一个响应列表。示例如下。

import grequests

req_list = [ # 请求列表
 grequests.get('http://httpbin.org/get?a=1&b=2'),
 grequests.post('http://httpbin.org/post', data={'a':1,'b':2}),
 grequests.put('http://httpbin.org/post', json={'a': 1, 'b': 2}),
]

res_list = grequests.map(req_list) # 并行发送,等最后一个运行完后返回
print(res_list[0].text) # 打印第一个请求的响应文本

grequests支持get、post、put、delete等requests支持的HTTP请求方法,使用参数和requests一致,发送请求非常简单。
通过遍历res_list可以得到所有请求的返回结果。

grequests和requests性能对比

我们可以对比下requests串行和grequests并行请求100次github.com的时间,示例如下。
使用requests发送请求

import requests
import time

start = time.time()
res_list = [requests.get('https://github.com') for i in range(100)]
print(time.time()-start)

实际耗时约100s+

使用grequests发送

import grequests
import time

start = time.time()
req_list = [grequests.get('https://github.com') for i in range(100)]
res_list = grequests.map(req_list)
print(time.time()-start)

际耗时约3.58s

异常处理

在批量发送请求时难免遇到某个请求url无法访问或超时等异常,grequests.map()方法还支持自定义异常处理函数,示例如下。

import grequests

def err_handler(request, exception):
 print("请求出错")

req_list = [
 grequests.get('http://httpbin.org/delay/1', timeout=0.001), # 超时异常
 grequests.get('http://fakedomain/'), # 该域名不存在
 grequests.get('http://httpbin.org/status/500') # 正常返回500的请求
]

res_list = grequests.map(reqs, exception_handler=err_handler)
print(res_list)

运行结果:

请求出错
请求出错
[None, None, <Response [500]>]

看完上述内容,你们掌握在Python中利用grequests实现一个并发请求功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI