[b]"What's the best way to implement python retry for API calls?"[/b] or [b]"How do you handle python retry logic

12 Replies, 1510 Views

"Python retry for API calls – what’s your go-to method?"

Hey folks!

I’ve been banging my head against retrying API calls in python and wanted to see how others handle it.

Do you use built-in stuff like `tenacity` or `retrying`? Or just roll your own decorator?

Sometimes the simple `for` loop with `try/except` feels messy, but idk if adding a lib is overkill.

Also, how do you handle backoff? Exponential? Jitter? Or just YOLO it with fixed delays?

Kinda curious what’s worked best for y’all. Share your python retry hacks!

(Also, if anyone’s got a clean way to avoid nested try-catch spaghetti, pls share lol)
I swear by `tenacity` for python retry logic—it’s super flexible and handles all the annoying stuff like backoff and jitter out of the box.

You can tweak it to stop after X attempts, add exponential delays, or even random jitter to avoid thundering herds.

For example:
```python
from tenacity import retry, stop_after_attempt, wait_exponential

@retry(stop=stop_after_attempt(3), wait=wait_exponential())
def call_api():
# your code here
```

Way cleaner than rolling your own loop IMO.
Honestly, I just use a simple decorator I wrote myself. Libraries are great, but sometimes you don’t need the extra dependency.

Here’s a barebones version:
```python
import time
import random

def retry(max_retries=3, delay=1):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(max_retries):
try:
return func(*args, **kwargs)
except Exception as e:
time.sleep(delay + random.uniform(0, 1)) # lil jitter
raise
return wrapper
return decorator
```

Works for most cases and no magic.
If you’re into async, check out `aiohttp_retry`. It’s built for async API calls and handles retries + backoff like a champ.

Also, +1 for jitter—it’s a lifesaver when you’ve got a ton of clients hitting the same endpoint.

For sync stuff, `requests` + `tenacity` is my go-to python retry combo.
I’ve been down this rabbit hole too! Ended up using `backoff` lib because it’s stupid simple and has sane defaults.

Example:
```python
import backoff
import requests

@backoff.on_exception(backoff.expo, requests.exceptions.RequestException, max_tries=3)
def get_data():
return requests.get("https://api.example.com")
```

Exponential backoff is baked in, and you can add jitter if you want.
OP here—wow, thanks for all the suggestions!

I tried `tenacity` and it’s exactly what I needed. The decorator makes it so clean, and the jitter option is 👌.

Still curious though—anyone have tips for logging retries? Like, how do you track failed attempts without spamming your logs?

Also, shoutout to the `backoff` lib suggestion—gonna test that next!
For those who hate decorators (🙋‍♂️), you can use `urllib3`’s built-in retry:
```python
from urllib3.util import Retry
from requests.adapters import HTTPAdapter

session = requests.Session()
retries = Retry(total=3, backoff_factor=1)
session.mount("https://", HTTPAdapter(max_retries=retries))
```

No fancy libs needed, and it’s pretty configurable.



Users browsing this thread: 1 Guest(s)