首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用韧性装饰器时,python3尝试/例外似乎失败

使用韧性装饰器时,python3尝试/例外似乎失败
EN

Stack Overflow用户
提问于 2020-09-08 06:23:41
回答 1查看 249关注 0票数 1

尝试使用请求函数来实现重试/回退,如果所有重试都失败,则返回HTTP状态。似乎我可以让重试工作,或者使用try/except,但不能同时使用这两种方法。包装在另一个函数中也无济于事。

OSX mojave上的Python 3.7.7请求2.24.0韧性6.2.0

带装饰器和不带try/except的函数1调用坚韧规则,但我没有得到http错误作为返回:

代码语言:javascript
复制
In [1330]: @retry(reraise=True,wait=wait_fixed(1), stop=stop_after_attempt(3))
      ...: def geturl(url):
      ...:     """
      ...:     get the url and raise http request errors for tenacity
      ...:     """
      ...:     print(datetime.now().strftime('%Y-%m-%d-%H:%M:%S'))
      ...:     headers = {'user-agent': 'my-app/0.0.1','Content-Type':'application/json'}
      ...:     data = {'name':'testcall','service':'armor'}
      ...:     r=None
      ...:     r = requests.post(url, data=json.dumps(data), headers=headers)
      ...:     r.raise_for_status()
      ...:     return r.status_code
      ...:

In [1331]: geturl(flakyurl)
2020-09-07-14:48:19
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): snout:7777
DEBUG:urllib3.connectionpool:http://snout:7777 "POST /flakyservice HTTP/1.1" 406 27
2020-09-07-14:48:20
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): snout:7777
DEBUG:urllib3.connectionpool:http://snout:7777 "POST /flakyservice HTTP/1.1" 405 27
2020-09-07-14:48:21
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): snout:7777
DEBUG:urllib3.connectionpool:http://snout:7777 "POST /flakyservice HTTP/1.1" 405 27
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
...

HTTPError: 405 Client Error: METHOD NOT ALLOWED for url: http://snout:7777/flakyservice

带有try/except的函数2获得了正确的错误,但无法调用韧度

代码语言:javascript
复制
In [1332]: @retry(reraise=True,wait=wait_fixed(1), stop=stop_after_attempt(3))
      ...: def geturl3(url):
      ...:         print(datetime.now().strftime('%Y-%m-%d-%H:%M:%S'))
      ...:         headers = {'user-agent': 'my-app/0.0.1','Content-Type':'application/json'}
      ...:         data = {'name':'testcall','service':'armor'}
      ...:         r=None
      ...:         try:
      ...:             r = requests.post(url, data=json.dumps(data), headers=headers)
      ...:             r.raise_for_status()
      ...:             return 'sent', r.status_code
      ...:         except requests.exceptions.HTTPError as err:
      ...:             return 'http failed', err
In [1334]: geturl3(flakyurl)
2020-09-07-15:23:00
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): snout:7777
DEBUG:urllib3.connectionpool:http://snout:7777 "POST /flakyservice HTTP/1.1" 404 27
Out[1334]:
('http failed',
 requests.exceptions.HTTPError('404 Client Error: NOT FOUND for url: http://snout:7777/flakyservice'))

编辑:修复方法是在异常之后使用r.raise_for_status() -这将重新激活并捕获韧性

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-12 07:26:31

在编辑后的问题中回答:修复方法是在异常之后使用r.raise_for_status() -这将重新激活并捕获韧性

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63784951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档