如何重试x次并在打印时长时间睡眠

2024-09-28 23:04:13 发布

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

我一直在试图理解backoff是如何工作的。我的目标是,每当我达到状态码等:405 5次。我想将睡眠时间设置为60000秒,并打印出发生状态错误405

现在我写了:

import time

import backoff
import requests

@backoff.on_exception(
    backoff.expo,
    requests.exceptions.RequestException,
    max_tries=5,
    giveup=lambda e: e.response is not None and e.response.status_code == 405
)
def publish(url):
    r = requests.post(url, timeout=10)
    r.raise_for_status()


publish("https://www.google.se/")

现在发生的事情是,如果它只达到405次,它将提升状态代码并停止脚本。我在寻找的是如何让脚本重试5次,如果状态是405行中的5次,那么我们需要长时间睡眠并打印出来。我如何使用回退来实现这一点?我还想听听其他建议:)

计数器的旧方法:

    import requests
    import time
    from requests.exceptions import ConnectionError, ReadTimeout, RequestException, Timeout
    
    exception_counter = 0
    
    while True:
    
        try:
            response = requests.get("https://stackoverflow.com/", timeout=12)
    
            if response.ok:
                print("Very nice")
                time.sleep(60)
    
            else:
                print(
                    f'[Response -> {response.status_code}]'
                    f'[Response Url -> {response.url}]'
                )
                time.sleep(60)
    
                if response.status_code == 403:
                    if exception_counter >= 10:
                        print("Hit limitation of counter: Response [403]")
                        time.sleep(4294968)
    
                    exception_counter += 1
    
        
    except (ConnectionError) as err:
        print(err)
        time.sleep(random.randint(1, 3))
        
        if exception_counter >= 10:
            print(f"Hit limitation of coonnectionerror {err}")
            time.sleep(4294968)
            continue
        
        exception_counter += 1
        continue
        
    except (ReadTimeout, Timeout) as err:
        print(err)
        time.sleep(random.randint(1, 3))
        continue

    except RequestException as err:
        print(err)
        time.sleep(random.randint(1, 3))
        continue

    except Exception as err:
        print(err)
        time.sleep(random.randint(1, 3))
        
        if exception_counter >= 10:
            print(f"Hit limitation of Exception {err}")
            time.sleep(4294968)
            continue
        
        exception_counter += 1
        continue

Tags: importiftimeresponse状态statuscounterexception
1条回答
网友
1楼 · 发布于 2024-09-28 23:04:13

睡了60000秒后,你没有说你想做什么,所以我将它设置为在四次尝试后睡觉,然后在失败之前做最后(第五次)尝试

您可以像使用on_backoff处理程序所要求的那样添加自定义逻辑

此外,我还重新调整了giveup函数,您可能使用了错误的布尔值

import time
import backoff
import requests


def backoff_hdlr(details):
    print("backoff_hdlr", details)
    if details["tries"] >= 4:
        print(f"sleeping")
        time.sleep(1)  # 60000


@backoff.on_exception(
    backoff.expo,
    requests.exceptions.RequestException,
    max_tries=5,
    giveup=lambda e: e.response.status_code != 405,
    on_backoff=backoff_hdlr,
)
def publish(url):
    print(f"called publish with url={url}")
    r = requests.post(url, timeout=10)
    r.raise_for_status()


publish("https://www.google.se/")
/Users/michael/.conda/envs/mip_typing/bin/python /Users/michael/git/mip_typing/scratch_2.py
called publish with url=https://www.google.se/
backoff_hdlr {'target': <function publish at 0x7fe45c626b80>, 'args': ('https://www.google.se/',), 'kwargs': {}, 'tries': 1, 'elapsed': 1.5e-05, 'wait': 0.8697943681459608}
called publish with url=https://www.google.se/
backoff_hdlr {'target': <function publish at 0x7fe45c626b80>, 'args': ('https://www.google.se/',), 'kwargs': {}, 'tries': 2, 'elapsed': 1.144912, 'wait': 1.5425500028676453}
called publish with url=https://www.google.se/
backoff_hdlr {'target': <function publish at 0x7fe45c626b80>, 'args': ('https://www.google.se/',), 'kwargs': {}, 'tries': 3, 'elapsed': 2.949183, 'wait': 0.2052666718206697}
called publish with url=https://www.google.se/
backoff_hdlr {'target': <function publish at 0x7fe45c626b80>, 'args': ('https://www.google.se/',), 'kwargs': {}, 'tries': 4, 'elapsed': 3.418447, 'wait': 5.113712077372433}
sleeping
called publish with url=https://www.google.se/
Traceback (most recent call last):
...

相关问题 更多 >