
您可以使用自定义的适配器,并对所有 http/https 请求强制执行指数退避因子多次重试。请参阅下面的示例:
import requestsfrom requests import adaptersfrom urllib3.util import retry# create a transport adapter with a custom retry strategy.retries = retry( total=3, backoff_factor=3, status_forcelist=[500, 502, 503, 504])adapter = adapters.httpadapter(max_retries=retries)# ensure adapter is used for both http and https requests.session = requests.session()session.mount('https://', adapter)session.mount('http://', adapter)# testing the retry mechanismresponse = session.get("http://httpbin.org/status/500")
这将返回以下错误:
RetryError: HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /status/500 (Caused by ResponseError('too many 500 error responses'))
不幸的是,似乎没有办法知道上述机制尝试了多少次重试,只有当所有尝试都已用尽时
参考
https://stackoverflow.com/a/47475019/4477547
以上就是直到“requests”库支持指数退避的自动重试的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1354687.html
微信扫一扫
支付宝扫一扫