Florent
Florent

Reputation: 1928

How to detect if retry fails with the Python retrying module?

I use the Python retrying library to wait and retry download when server is in DDoS protection, but my question is how can I throw an exception when all retries fail ?

In my code the download(symbol) function might raise a DDoSProtection exception. If it is the case I would like to start the retry and if stop_max_attempt_number retries fail raise the downloadError() exception.

def retry_if_ddospro_error(exception):

    """Return True if we should retry (in this case when it's an DDoSProtection), False otherwise"""

    return isinstance(exception, DDoSProtection)


@retry(retry_on_exception = retry_if_ddospro_error, stop_max_attempt_number = 3, wait_fixed=3000)

def download (symbol): 

    ls = exchange.fetch_ohlcv(symbol) # Might raise DDoSProtection

    # RETRY

    if retry_fail:

        # RAISE new exception in order to log error into database 

        raise downloadError('Exchange is in DDoS protection')

EDIT : removing the try except in my example.

References: https://pypi.python.org/pypi/retrying

Upvotes: 2

Views: 460

Answers (0)

Related Questions