简体   繁体   English

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

[英]How to retry x times and put a long sleep with a print

I have been trying to understand how backoff works.我一直在试图了解退避是如何工作的。 My goal is that whenever I reach a status_code etc: 405 5 times.我的目标是每当我达到 status_code 等:405 5 次。 I want to put a sleep of 60000 sec and print out that there has occured status error 405.我想休眠 60000 秒并打印出出现状态错误 405。

Right now I have written:现在我已经写了:

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/")

and what happens now is that if it just reaches 405 once, it will raise the status_code and stop the script.现在发生的情况是,如果它只达到 405 一次,它将引发 status_code 并停止脚本。 What im looking for is how I can make the script to retry 5 times, if the status is 5 times in a row of 405, then we want to put a long sleep and print it out.我正在寻找的是如何让脚本重试 5 次,如果状态是 405 连续 5 次,那么我们想要长时间睡眠并打印出来。 How can I do that using backofF?如何使用 backofF 做到这一点? Im also up for other suggestions:)我也想提出其他建议:)

The old way of counter:旧的计数器方式:

    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

You didn't say what you want to do after sleeping for 60000 seconds, so I set it up to sleep after four attempts, and then do one final (fifth) attempt before failing proper.您没有说在睡眠 60000 秒后要做什么,所以我将其设置为在四次尝试后睡眠,然后在正确失败之前进行最后一次(第五次)尝试。

You can add custom logic like you've asked for with an on_backoff handler.您可以使用on_backoff处理程序添加您要求的自定义逻辑。

Also I rejigged your giveup function, you might have had the boolean around the wrong way.此外,我重新调整了您的giveup ,您可能以错误的方式使用了 boolean。

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):
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM