Skip to main content

Python Retry Decorator

Python

Retry a flaky call a fixed number of times.

E
Emeka Nwosu
Sample Sep 9, 2026

Remixed from Exponential Backoff with Jitter by Zainab Bello

Python · 11 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def retry(times=3):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for attempt in range(times):
                try:
                    return func(*args, **kwargs)
                except Exception:
                    if attempt == times - 1:
                        raise
        return wrapper
    return decorator
2 Fire 0 Learned something 0 Saved me time 4 Mind-blown
Sign in to react and star