Python2 异步网络请求库比较

本文对比了Python2中多个异步网络请求库,包括grequests、requests-threads、txrequests、treq和trip。这些库分别基于gevent、线程和Twisted框架,提供了异步请求的功能。例如,grequests利用gevent实现,而treq则旨在提供与requests库相似的接口。此外,还介绍了底层库gevent,它是一个常用的Python2异步库,通过monkey patch将网络请求转换为异步模式。
摘要由CSDN通过智能技术生成

封装库

grequests

简介

requests作者写的基于gevent的异步请求库。

地址

https://github.com/kennethreitz/grequests

示例
import grequests

urls = [
    'http://www.heroku.com',
    'http://python-tablib.org',
    'http://httpbin.org',
    'http://python-requests.org',
    'http://fakedomain/',
    'http://kennethreitz.com'
]
rs = (grequests.get(u) for u in urls)
grequests.map(rs)

get方法参数与requests.get()参数相同,当然也支持postputdelete等方法。
请求失败默认返回None,可以给map方法传递。exception_handler参数指定请求失败的回调函数。

map支持的参数如下:

:param requests: a collection of Request objects.
:param stream: If True, the content will not be downloaded immediately.
:param size: Specifies the number of requests to make at a time. If None, no throttling occurs.
:param exception_h
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值