【发布时间】:2021-05-29 13:59:43
【问题描述】:
我一直试图了解backoff 的工作原理。我的目标是每当我达到 status_code 等: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 一次,它将引发 status_code 并停止脚本。我正在寻找的是如何让脚本重试 5 次,如果状态是 405 连续 5 次,那么我们想要长时间睡眠并打印出来。如何使用 backofF 做到这一点?我也有其他建议:)
旧式计数器:
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
【问题讨论】:
标签: python python-3.x if-statement exception