

def _with_retry(function, verbose=False, retry_status_codes=[429, 502, 503, 504], retry_errors=[], retry_exceptions=[], retries=3, wait=1, back_off=2, max_wait=30):
    '\n    Retries the given function under certain conditions.\n    \n    :param function:           A function with no arguments.  If arguments are needed, use a lambda (see example).  \n    :param retry_status_codes: What status codes to retry upon in the case of a SynapseHTTPError.\n    :param retry_errors:       What reasons to retry upon, if function().response.json()[\'reason\'] exists.\n    :param retry_exceptions:   What types of exceptions, specified as strings, to retry upon.\n    :param retries:            How many times to retry maximum.\n    :param wait:               How many seconds to wait between retries.  \n    :param back_off:           Exponential constant to increase wait for between progressive failures.  \n    \n    :returns: function()\n    \n    Example::\n        \n        def foo(a, b, c): return [a, b, c]\n        result = self._with_retry(lambda: foo("1", "2", "3"), **STANDARD_RETRY_PARAMS)\n    '
    while True:
        exc_info = None
        retry = False
        response = None
        try:
            response = function()
        except Exception as ex:
            exc_info = sys.exc_info()
            if verbose:
                sys.stderr.write((str(ex.message) + '\n'))
            if hasattr(ex, 'response'):
                response = ex.response
        if (response is not None):
            if (response.status_code in retry_status_codes):
                if verbose:
                    sys.stderr.write(('retrying on status code: \n' + str(response.status_code)))
                retry = True
            elif (response.status_code not in range(200, 299)):
                if _is_json(response.headers.get('content-type', None)):
                    try:
                        json = response.json()
                        if ('Please slow down.  You may send a maximum of 10 message' in json.get('reason', None)):
                            if verbose:
                                sys.stderr.write((('retrying' + json.get('reason', None)) + '\n'))
                            retry = True
                            wait = 16
                        elif any([(msg.lower() in json.get('reason', None).lower()) for msg in retry_errors]):
                            if verbose:
                                sys.stderr.write((('retrying' + json.get('reason', None)) + '\n'))
                            retry = True
                    except (AttributeError, ValueError) as ex:
                        pass
                elif any([(msg.lower() in response.text.lower()) for msg in retry_errors]):
                    if verbose:
                        sys.stderr.write((('retrying' + response.text) + '\n'))
                    retry = True
        if (exc_info is not None):
            if ((exc_info[1].__class__.__name__ in retry_exceptions) or any([(msg.lower() in str(exc_info[1]).lower()) for msg in retry_errors])):
                if verbose:
                    sys.stderr.write(((('retrying exception: ' + exc_info[1].__class__.__name__) + str(exc_info[1])) + '\n'))
                retry = True
        retries -= 1
        if ((retries >= 0) and retry):
            randomized_wait = (wait * random.uniform(0.5, 1.5))
            if verbose:
                sys.stderr.write('\n... Retrying in {wait} seconds...\n'.format(wait=randomized_wait))
            time.sleep(randomized_wait)
            wait = min(max_wait, (wait * back_off))
            continue
        if ((exc_info is not None) and (exc_info[0] is not None)):
            print(exc_info[0])
            print(exc_info[1])
            print(exc_info[2])
            raise exc_info[0](ex[1])
        return response

dataset/ETHPy150Open Sage-Bionetworks/synapsePythonClient/synapseclient/retry.py _with_retry/VarMisuse@666/1203 `exc_info`->`ex`