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

守給你的承諾、 提交于 2019-12-03 03:18:39

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