DEV Community

Yaroslav Polyakov
Yaroslav Polyakov

Posted on

2

Simple decorator to retry after exception

Typical situation: you have unreliable function, for example, doing HTTP requests and getting exception from time to time. Maybe connection went down temporary or 429 Too Many requests problem, or remote service itself is temporary broken.

One simple decorator solves problem:

import functools

def retry(num_times=1):
    def decorator_repeat(func):
        @functools.wraps(func)
        def wrapper_repeat(*args, **kwargs):
            last_exception = None
            for n in range(num_times):
                try:
                    r = func(*args, **kwargs)
                except Exception as e:
                    last_exception = e
                    # handle exception. log it or just pass
                    print(f"Try #{n} got exception {e}, but we will retry")
                else:
                    return r
            raise last_exception
        return wrapper_repeat
    return decorator_repeat

Enter fullscreen mode Exit fullscreen mode

Lets use it with requests:

import requests

@retry(10)
def myhttp():
    r = requests.get('https://httpbin.org/status/200,500,429')
    r.raise_for_status()
    print(r.status_code)

myhttp()
Enter fullscreen mode Exit fullscreen mode

Results:

Try #0 got exception 429 Client Error: TOO MANY REQUESTS for url: https://httpbin.org/status/200,500,429, but we will retry
Try #1 got exception 500 Server Error: INTERNAL SERVER ERROR for url: https://httpbin.org/status/200,500,429, but we will retry
Try #2 got exception 500 Server Error: INTERNAL SERVER ERROR for url: https://httpbin.org/status/200,500,429, but we will retry
200
Enter fullscreen mode Exit fullscreen mode

This way you are immune against temporary problems which could be solved by retry (maybe with time.sleep(10)) but you will still get exception from long-time problems. Change URL in myhttp() to https://httpbin.org/status/500 to see this.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay