
Tried 3 ways to handle retries in Python. This one actually makes sense.
Tried 3 ways to handle retries in Python. This one actually makes sense. Scraping APIs that randomly fail taught me retries matter. Tried a bunch of approaches. Most were garbage. Manual While Loop (My First Attempt) Started obvious: attempts = 0 max_attempts = 3 while attempts < max_attempts : try : response = requests . get ( url ) response . raise_for_status () break except requests . exceptions . RequestException : attempts += 1 if attempts >= max_attempts : raise Worked for one endpoint. Copy pasted this everywhere. Had 15 files with the same loop. Changed retry logic once, had to update 15 files. Dumb. Tenacity Library (Overkill) Found the tenacity library. Looked professional: from tenacity import retry , stop_after_attempt , wait_exponential @retry ( stop = stop_after_attempt ( 3 ), wait = wait_exponential ( multiplier = 1 , min = 2 , max = 10 )) def fetch_data ( url ): response = requests . get ( url ) response . raise_for_status () return response . json () This worked fine b
Continue reading on Dev.to Tutorial
Opens in a new tab



