用grequests实现并发http请求

来源:互联网 发布:二维数组赋值 scanf 编辑:程序博客网 时间:2024/05/12 18:17

起因

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

1. 简单用法

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

import grequestsimport timeimport requestsurls = [    '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 - t1def 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 - t1def 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.51106119156method2 5.77834510803method3 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)

参考资料:

  1. Requests 事件钩子
原创粉丝点击