Python使用grequests(gevent+requests)并发发送请求过程解析

yipeiwu_com6年前Python基础

前言

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]>]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 计算文件的md5值实例

较小文件处理方法: import hashlib import os def get_md5_01(file_path): md5 = None if os.path.is...

Python中列表的一些基本操作知识汇总

Python中列表的一些基本操作知识汇总

 Python最基本的数据结构是序列(列表/元组)。一个序列中的每个元素都分配有一个数字- 它的位置或索引。第一个索引是0,第二个索引是1,依此类推。 Python有6内置类型...

Python实现向QQ群成员自动发邮件的方法

本文实例讲述了Python实现向QQ群成员自动发邮件的方法。分享给大家供大家参考。具体实现方法如下: 原理: 我们需要先获取QQ群中的所有成员并保存到一个txt文本中去,然后再由pyth...

Python使用matplotlib实现的图像读取、切割裁剪功能示例

Python使用matplotlib实现的图像读取、切割裁剪功能示例

本文实例讲述了Python使用matplotlib实现的图像读取、切割裁剪功能。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- import sys...

浅谈python数据类型及类型转换

Python中核心的数据类型有哪些? 变量(数字、字符串、元组、列表、字典) 什么是数据的不可变性?哪些数据类型具有不可变性 数据的不可变是指数据不可更改,比如: a = ("ab...