如何在python请求库中实现重试机制?

sor*_*rin 34 python http python-requests

我想为python请求库添加一个重试机制,因此使用它的脚本将重试非致命错误.

此刻我确实认为有些错误是可以恢复的:

  • HTTP返回码502,503,504
  • 主机未找到(现在不太重要)
  • 请求超时

在第一阶段,我确实想每分钟重试指定的5xx请求.

我希望能够透明地添加此功能,而无需为使用python-requests的这些脚本或库中的每个HTTP调用手动实现恢复.

dat*_*man 87

这段代码将使来自同一会话的所有HTTP请求重试总共5次,在重试之间休眠,增加后退0s,2s,4s,8s,16s(第一次重试立即完成).它将重试基本连接问题(包括DNS查找失败)和HTTP状态代码502,503和504.

import logging
import requests

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

logging.basicConfig(level=logging.DEBUG)

s = requests.Session()
retries = Retry(total=5, backoff_factor=1, status_forcelist=[ 502, 503, 504 ])
s.mount('http://', HTTPAdapter(max_retries=retries))

s.get("http://httpstat.us/503")
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅重试类.

  • 在 Retry 类中查找 `method_whitelist` 选项,默认情况下它不会重试除 `['HEAD', 'TRACE', 'GET', 'PUT', 'OPTIONS', 'DELETE']` 之外的 http 方法- 这些方法预计不会有副作用 (5认同)
  • session.mount, `http://` 的第一个参数的意义是什么?它的目的是什么? (4认同)
  • @JamesWierzba是连接到适配器的一种模式。因此,在这种情况下,所有以“ http://”开头的URL都将使用该适配器。例如,您可能具有用于https://cnn.com的特定适配器。当访问`https:// cnn.com`时,所有使用该会话的呼叫都将通过自定义HTTP适配器进行访问,而其他所有访问都将使用默认值。 (3认同)
  • 设置 method_whitelist=False 以重试 POST API 调用。例如,如果您正在与 GraphQL 端点通信,则所有调用都是 POST。默认情况下,仅重试 ['HEAD'、'TRACE'、'GET'、'PUT'、'OPTIONS'、'DELETE'],因为它们被视为安全/幂等操作。 (3认同)
  • Retry 类在内部处理。如果所有重试都失败(或重试被禁用),它会抛出 https://urllib3.readthedocs.io/en/latest/reference/index.html#urllib3.exceptions.MaxRetryError 你可以根据需要捕获它。我已经更新了 Retry 类的链接 - 查看那里了解更多详细信息。 (2认同)
  • 根据文档,第一次退避是0,而不是1。就像0s,2s,4s,8s,16s一样 (2认同)

Dan*_*kin 7

使用重试包的可能解决方案

from retrying import retry
import requests


def retry_if_connection_error(exception):
    """ Specify an exception you need. or just True"""
    #return True
    return isinstance(exception, ConnectionError)

# if exception retry with 2 second wait  
@retry(retry_on_exception=retry_if_connection_error, wait_fixed=2000)
def safe_request(url, **kwargs):
    return requests.get(url, **kwargs)

response = safe_request('test.com')
Run Code Online (Sandbox Code Playgroud)


Mik*_*que 5

这是我用来重试urllib2的请愿的代码片段。也许您可以将其用于您的目的:

retries = 1
success = False
while not success:
    try:
        response = urllib2.urlopen(request)
        success = True
    except Exception as e:
        wait = retries * 30;
        print 'Error! Waiting %s secs and re-trying...' % wait
        sys.stdout.flush()
        time.sleep(wait)
        retries += 1
Run Code Online (Sandbox Code Playgroud)

等待时间逐渐增加,以避免被服务器禁止。

  • 为了限制重试次数,我可以使用 `while not success and retries < 5:` (11认同)
  • 我宁愿在循环中明确使用“继续”并默认中断。只是为了避免错误地无限循环。 (6认同)
  • 您的答案是一个简单的尝试 - 除了没有调用不同库和函数的公式。 (2认同)

sar*_*t07 5

from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry


MAX_RETRY = 2
MAX_RETRY_FOR_SESSION = 2
BACK_OFF_FACTOR = 0.3
TIME_BETWEEN_RETRIES = 1000
ERROR_CODES = (500, 502, 504)


def requests_retry_session(retries=MAX_RETRY_FOR_SESSION,
    back_off_factor=BACK_OFF_FACTOR,
    status_force_list=ERROR_CODES, 
    session=None):
       session = session  
       retry = Retry(total=retries, read=retries, connect=retries,
                     backoff_factor=back_off_factor,
                     status_forcelist=status_force_list,
                     method_whitelist=frozenset(['GET', 'POST']))
       adapter = HTTPAdapter(max_retries=retry)
       session.mount('http://', adapter)
       session.mount('https://', adapter)
       return session



class ConfigService:

   def __init__(self):
      self.session = requests_retry_session(session=requests.Session())

   def call_to_api():
      config_url = 'http://localhost:8080/predict/'
      headers = {
        "Content-Type": "application/json",
        "x-api-key": self.x_api_key
      } 
      response = self.session.get(config_url, headers=headers)
      return response
Run Code Online (Sandbox Code Playgroud)


sor*_*rin -1

通过扩展课程,我能够获得所需的可靠性水平requests.Session。

这是代码https://bitbucket.org/bspeakmon/jira-python/src/a7fca855394402f58507ca4056de87ccdbd6a213/jira/resilientsession.py?at=master

编辑该代码是:

from requests import Session
from requests.exceptions import ConnectionError
import logging
import time


class ResilientSession(Session):

    """
    This class is supposed to retry requests that do return temporary errors.

    At this moment it supports: 502, 503, 504
    """

    def __recoverable(self, error, url, request, counter=1):
        if hasattr(error,'status_code'):
            if error.status_code in [502, 503, 504]:
                error = "HTTP %s" % error.status_code
            else:
                return False
        DELAY = 10 * counter
        logging.warn("Got recoverable error [%s] from %s %s, retry #%s in %ss" % (error, request, url, counter, DELAY))
        time.sleep(DELAY)
        return True


    def get(self, url, **kwargs):
        counter = 0
        while True:
            counter += 1
            try:
                r = super(ResilientSession, self).get(url, **kwargs)
            except ConnectionError as e:
                r = e.message
            if self.__recoverable(r, url, 'GET', counter):
                continue
            return r

    def post(self, url, **kwargs):
        counter = 0
        while True:
            counter += 1
            try:
                r = super(ResilientSession, self).post(url, **kwargs)
            except ConnectionError as e:
                r = e.message
            if self.__recoverable(r, url, 'POST', counter):
                continue
            return r

    def delete(self, url, **kwargs):
        counter = 0
        while True:
            counter += 1
            try:
                r = super(ResilientSession, self).delete(url, **kwargs)
            except ConnectionError as e:
                r = e.message
            if self.__recoverable(r, url, 'DELETE', counter):
                continue
            return r

    def put(self, url, **kwargs):
        counter = 0
        while True:
            counter += 1
            try:
                r = super(ResilientSession, self).put(url, **kwargs)
            except ConnectionError as e:
                r = e.message

            if self.__recoverable(r, url, 'PUT', counter):
                continue
            return r

    def head(self, url, **kwargs):
        counter = 0
        while True:
            counter += 1
            try:
                r = super(ResilientSession, self).head(url, **kwargs)
            except ConnectionError as e:
                r = e.message
            if self.__recoverable(r, url, 'HEAD', counter):
                continue
            return r

    def patch(self, url, **kwargs):
        counter = 0
        while True:
            counter += 1
            try:
                r = super(ResilientSession, self).patch(url, **kwargs)
            except ConnectionError as e:
                r = e.message

            if self.__recoverable(r, url, 'PATCH', counter):
                continue
            return r

    def options(self, url, **kwargs):
        counter = 0
        while True:
            counter += 1
            try:
                r = super(ResilientSession, self).options(url, **kwargs)
            except ConnectionError as e:
                r = e.message

            if self.__recoverable(r, url, 'OPTIONS', counter):
                continue
            return r
Run Code Online (Sandbox Code Playgroud)

  • 我知道这段代码已有 5 年历史,但其质量非常低。应该接受@datashaman的答案,这样这个答案就可以受到更少的关注。 (4认同)
  • 这是使用装饰器对弹性会话进行基本重写。仍然有一些重复(所有方法都具有相同的模式),但它的工作原理完全相同,您只需在一处更改逻辑:https://gist.github.com/datashaman/fc02882d6be49d0b882f (3认同)