Tuesday, December 19, 2023

Tenacity with Asyncio - Defensive Programming

from tenacity import (retry
,stop_after_attempt
,wait_fixed
,retry_if_exception_type
,stop_after_delay)
import httpx
import asyncio
import socket
@retry(stop=(stop_after_delay(3) | stop_after_attempt(5)),wait=wait_fixed(2),retry=retry_if_exception_type(Exception))
async def stop_after_7_attempts(): #stop_after_10s_or_5_retries
print("Stopping after 7 attempts")
resp=httpx.get("https://www.youtube.com/aasd")
if (resp.status_code == 404): raise Exception("404 Not working")
print(" trying ... " )
asyncio.run(stop_after_7_attempts())
view raw tenacity.py hosted with ❤ by GitHub