使用grequests发送请求池,如何获得每个请求的响应时间?

时间:2012-12-10 21:35:35

标签: python-requests grequests

我正在使用grequests python库将GET请求异步发送到我们的服务器。

我无法弄清楚如何在发出的请求池中获取每个请求的服务器响应时间?

unsentrequests=(grequests.get(u) for u in self.urls) # make a pool of requests
responses=grequests.map(unsentrequests) # send the requests asynchronously

要获取请求 - 响应对的开始时间,我可以执行以下操作:

grequests.get(u,headers={'start':time.time())
print responses[0].request.headers['start_time']

但我如何才能收到回复的时间?

1 个答案:

答案 0 :(得分:13)

grequestsrequests一样,支持hook关键字参数,您可以在其中指定一个对响应对象执行某些操作的函数:

def do_something(response):
    print response.status_code

unsentrequests=(grequests.get(u, hooks = {'response' : do_something}) for u in self.urls) 
responses=grequests.map(unsentrequests)

我更喜欢在使用requests的循环中直接使用gevent来获得更明确的代码:

import gevent.monkey
gevent.monkey.patch_socket()
from gevent.pool import Pool
import requests

def check_urls(urls):

    def fetch(url):
        response = requests.request('GET', url, timeout=5.0)
        print "Status: [%s] URL: %s" % (response.status_code, url)

    pool = Pool(20)
    for url in urls:
        pool.spawn(fetch, url)
    pool.join()