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

2024-09-30 20:28:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在我的代码中,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')

编辑:删除我示例中的tryexcept。在

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


Tags: innumberifdownloaddefexceptionerrorsymbol