Search code examples
python-requestsgrequests

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


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?


Solution

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