Python使用grequests(gevent+requests)并发发送请求过程解析
作者:韩志超 时间:2023-08-14 16:29:01
前言
requests是Python发送接口请求非常好用的一个三方库,由K神编写,简单,方便上手快。但是requests发送请求是串行的,即阻塞的。发送完一条请求才能发送另一条请求。
为了提升测试效率,一般我们需要并行发送请求。这里可以使用多线程,或者协程,gevent或者aiohttp,然而使用起来,都相对麻烦。
grequests是K神基于gevent+requests编写的一个并发发送请求的库,使用起来非常简单。
安装方法: pip install gevent grequests
项目地址:https://github.com/spyoungtech/grequests
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]>]
来源:https://www.cnblogs.com/superhin/p/11583560.html
标签:python,gevent,requests,并发,请求
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Yii2中的场景(scenario)和验证规则(rule)详解
2024-04-28 09:43:50
Sql Server "用户登录失败,错误编18456"的解决过程
2024-01-26 20:03:46
![](https://img.aspxhome.com/file/2023/8/99948_0s.png)
MySQL数据库优化之索引实现原理与用法分析
2024-01-27 08:12:37
![](https://img.aspxhome.com/file/2023/9/112629_0s.png)
Python实现求最大公约数及判断素数的方法
2021-06-13 02:20:20
PHP cookie,session的使用与用户自动登录功能实现方法分析
2023-11-21 15:00:43
python去掉字符串中重复字符的方法
2022-11-23 09:17:35
利用Python制作动态排名图的实现代码
2022-02-04 07:40:51
![](https://img.aspxhome.com/file/2023/7/128477_0s.gif)
python计算两个地址之间的距离方法
2023-09-01 01:40:59
Python学习之名字,作用域,名字空间(下)
2021-04-28 05:45:20
django Serializer序列化使用方法详解
2022-07-17 12:42:24
Django发送html邮件的方法
2021-04-06 13:22:52
在Linux命令行终端中使用python的简单方法(推荐)
2021-10-27 23:29:21
![](https://img.aspxhome.com/file/2023/1/133801_0s.jpg)
详解python 注释、变量、类型
2023-10-27 10:01:18
Python监听键盘和鼠标事件的示例代码
2022-06-14 07:05:23
![](https://img.aspxhome.com/file/2023/0/124790_0s.png)
MySQL 中查找含有目标字段的表的方法
2024-01-12 16:45:20
python性能检测工具函数运行内存及运行时间
2021-11-24 00:09:27
![](https://img.aspxhome.com/file/2023/8/80818_0s.png)
详解Vue 全局引入bass.scss 处理方案
2024-06-05 10:03:08
Python SDK实现私服上传下载的示例
2021-11-22 08:14:42
golang的强制类型转换实现
2024-01-31 03:29:38
Javascript typeof与instanceof的区别
2024-06-07 15:53:47