用GREQUESTS实现并发HTTP请求

13 篇文章 4 订阅
3 篇文章 1 订阅

起因

要用http请求探测服务的有效性,多进程,多线程,感觉似乎没有必要,看看有没有协程的方案

1. 简单用法

grequests 利用 requests和gevent库,做了一个简单封装,使用起来非常方便

import grequests
import time
import requests
urls = [
    'https://docs.python.org/2.7/library/index.html',
    'https://docs.python.org/2.7/library/dl.html',
    'http://www.iciba.com/partial',
    'http://2489843.blog.51cto.com/2479843/1407808',
    'http://blog.csdn.net/woshiaotian/article/details/61027814',
    'https://docs.python.org/2.7/library/unix.html',
    'http://2489843.blog.51cto.com/2479843/1386820',
    'http://www.bazhuayu.com/tutorial/extract_loop_url.aspx?t=0',
]

def method1():
    t1 = time.time()
    for url in urls:
        res = requests.get(url)
        #print res.status_code

    t2 = time.time()
    print 'method1', t2 - t1

def method2():
    tasks = [grequests.get(u) for u in urls]
    t1 = time.time()
    res = grequests.map(tasks, size=3)
#    print res
    t2 = time.time()
    print 'method2', t2 - t1

def method3():
    tasks = [grequests.get(u) for u in urls]
    t1 = time.time()
    res = grequests.map(tasks, size=6)
#    print res
    t2 = time.time()

if __name__ == '__main__':
    method1()
    method2()
    method3()

运行结果如下:

method1 8.51106119156
method2 5.77834510803
method3 2.55373907089

可以看到使用协程以后,整个程序的完成时间有了大大缩短,并且每个协程的并发粒度也会影响整体时间

2. 重要参数

这里需要补充的是几个
grequests

def grequests.map(requests, stream=False, size=None, exception_handler=None, gtimeout=None)
参数说明备注
size协程的并发度当一个协程进行IO等待的时候,就会将CPU交给其他协程序,一般设置为50 ~ 100足矣
exception_handler协程的并发度捕获单个请求的异常
gtimeout整体请求的超时设置 

另外,由于grequests底层使用的是requests,因此它支持
GET,OPTIONS, HEAD, POST, PUT, DELETE 等各种http method
所以以下的任务请求都是支持的

grequests.post(url, json={"name":"zhangsan"}) 
grequests.delete(url) 

3. 事件钩子

grequests的底层库,是requests,因此它也支持事件钩子

def print_url(r, *args, **kwargs):
        print(r.url)

url = "http://www.baidu.com"
# 1.
res = requests.get(url, hooks={"response":print_url})

# 2.
tasks = []
req = grequests.get(url, callback=print_url)
tasks.append(req)
res = grequests.map(tasks)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值