When working with external services and APIs in Python, dealing with failed requests is inevitable. However, with some thoughtful error handling and retry logic, you can make your Python application much more resilient. Here are some best practices for handling failed requests:
Use Try/Except Blocks to Catch Errors
Wrapping API calls or network requests in
try:
response = requests.get('http://api.example.com/data')
except requests.exceptions.Timeout:
# Timeout occurred
pass
except requests.exceptions.HTTPError:
# Unsuccessful status code returned
pass
This makes error handling explicit instead of failing silently.
Exponential Backoff for Retries
When retrying failed requests, use an exponential backoff algorithm that gradually increases the wait time between retries. This avoids bombarding unstable APIs. A simple implementation with the
retry_count = 0
retry_delay = 1 # seconds
while True:
try:
response = requests.get(url)
break
except Exception:
if retry_count < 5:
time.sleep(retry_delay)
retry_delay *= 2
retry_count += 1
else:
raise
Circuit Breaker Pattern
Once failures reach a threshold, use a "circuit breaker" to stop making requests for a period of time to allow the API to recover before trying again. This prevents wasting resources on recurring failed calls.
By planning for and handling failures systematically, you can build Python programs that gracefully handle unreliable APIs and network issues. The key is having clear error handling flow and not retrying endlessly.