我一直在试图理解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
睡了60000秒后,你没有说你想做什么,所以我将它设置为在四次尝试后睡觉,然后在失败之前做最后(第五次)尝试
您可以像使用
on_backoff
处理程序所要求的那样添加自定义逻辑此外,我还重新调整了
giveup
函数,您可能使用了错误的布尔值相关问题 更多 >
编程相关推荐