如何使用Python重试模块检测重试是否失败?

时间:2019-04-19 06:31:07

标签: python python-3.x exception retrying

当服务器处于DDoS保护状态时,我使用Python retrying库等待并重试下载,但是我的问题是当所有重试均失败时如何抛出异常?

在我的代码中,download(symbol)函数可能会引发DDoSProtection异常。如果是这种情况,我想开始重试,如果stop_max_attempt_number重试失败,则引发downloadError()异常。

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')

编辑:在我的示例中删除try except

参考:https://pypi.python.org/pypi/retrying

0 个答案:

没有答案