Using grequests to send a pool of requests, how can I get the response time of each individual request?

坚强是说给别人听的谎言 提交于 2019-12-03 13:18:55

问题


I am using the grequests python library to send GET requests asynchronously to our server.

I cant figure out how to get the server response time for each individual request within the pool of requests being sent out?

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

To get the start time of a request-response pair I could do the following:

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

But how can I grap the time that the response was received at?


回答1:


grequests, like requests, support an hook keyword argument where you can assign a function that does something with the response object:

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)

I prefer to use requests directly in a loop using gevent to have a more explicit code:

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()


来源:https://stackoverflow.com/questions/13809650/using-grequests-to-send-a-pool-of-requests-how-can-i-get-the-response-time-of-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!