具有指数退避的 Grequests

Grequests with exponential backoff

以下是我处理并发请求的模式:

rs = (grequests.get(url) for url in urls)
res_items = grequests.map(rs)
for num, res in enumerate(res_items):
    json_data = json.loads(res.text)

但是,这大约每 5,000 个请求就会崩溃并出现错误 ConnectionError: HTTPConnectionPool(host='apicache.vudu.com', port=80): Max retries exceeded with url:。执行上述操作的更可靠模式是什么——例如,如果单个请求不起作用,则重试 url 最多五次?

这是一个选项,使用此处所述的指数退避:

def grequester(self, url, n=1):
    '''
    Google exponential backoff: https://developers.google.com/drive/web/handle-errors?hl=pt-pt
    '''
    MAX_TRIES = 8
    try:
        res = grequests.get(url)
    except:
        if n > MAX_TRIES:
            return None
        n += 1
        log.warning('Try #%s for %s...' % (n, url))
        time.sleep((2 ** n) + (random.randint(0, 1000) / 1000.0))  # add jitter 0-1000ms
        return self.grequester(url, n)
    else:
        return res