我可以为 requests.request 设置 max_retries 吗? [英] Can I set max_retries for requests.request?

查看:35
本文介绍了我可以为 requests.request 设置 max_retries 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python requests 模块简单而优雅,但有一件事让我烦恼.可能会收到 requests.exception.ConnectionError 的消息,例如:

The Python requests module is simple and elegant but one thing bugs me. It is possible to get a requests.exception.ConnectionError with a message like:

Max retries exceeded with url: ...

这意味着请求可以多次尝试访问数据.但是在文档的任何地方都没有提到这种可能性.查看源代码,我没有找到任何可以更改默认值(大概为 0)的地方.

This implies that requests can attempt to access the data several times. But there is not a single mention of this possibility anywhere in the docs. Looking at the source code I didn't find any place where I could alter the default (presumably 0) value.

那么是否有可能以某种方式设置请求的最大重试次数?

So is it possible to somehow set the maximum number of retries for requests?

推荐答案

重试是底层的 urllib3 库.要设置不同的最大重试次数,请使用替代传输适配器:

It is the underlying urllib3 library that does the retrying. To set a different maximum retry count, use alternative transport adapters:

from requests.adapters import HTTPAdapter

s = requests.Session()
s.mount('http://stackoverflow.com', HTTPAdapter(max_retries=5))

max_retries 参数采用整数或 Retry() 对象;后者使您可以对重试何种类型的失败进行细粒度控制(一个整数值被转换为一个 Retry() 实例,它只处理连接失败;建立连接后的错误默认不是处理,因为这些可能会导致副作用).

The max_retries argument takes an integer or a Retry() object; the latter gives you fine-grained control over what kinds of failures are retried (an integer value is turned into a Retry() instance which only handles connection failures; errors after a connection is made are by default not handled as these could lead to side-effects).

旧答案,早于请求 1.2.1 的发布:

requests 库并没有真正使这个可配置,也不打算(参见 此拉取请求).当前(请求 1.1),重试次数设置为 0.如果您真的想将其设置为更高的值,则必须全局设置:

The requests library doesn't really make this configurable, nor does it intend to (see this pull request). Currently (requests 1.1), the retries count is set to 0. If you really want to set it to a higher value, you'll have to set this globally:

import requests

requests.adapters.DEFAULT_RETRIES = 5

这个常量没有记录;使用它需自行承担风险,因为未来的版本可能会改变处理方式.

This constant is not documented; use it at your own peril as future releases could change how this is handled.

更新:这确实改变了;在 1.2.1 版中 设置max_retries 参数的选项HTTPAdapter() 已添加,因此现在您必须使用替代传输适配器,请参见上文.猴子补丁方法不再有效,除非您还补丁 HTTPAdapter.__init__() 默认值(非常不推荐).

Update: and this did change; in version 1.2.1 the option to set the max_retries parameter on the HTTPAdapter() class was added, so that now you have to use alternative transport adapters, see above. The monkey-patch approach no longer works, unless you also patch the HTTPAdapter.__init__() defaults (very much not recommended).

这篇关于我可以为 requests.request 设置 max_retries 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆