Quick actions

cmd+k|ctrl+k

Navigation

Languages

Retry - Function Decorator

Snippet info

Language

Python

Visibility

public

Author

rajivm1991

Created

2023-04-24T04:39:04.379656Z

Updated

2023-04-24T04:39:04.379656Z

import functools


def retry(on: list, limit: int):
    def decorator_retry(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            i = 0
            while i < limit:
                try:
                    return func(*args, **kwargs)
                    break
                except Exception as e:
                    if avoid and not any(x in str(e) for x in avoid):
                        print('Retrying:', e)
                    else:
                        raise e
                i += 1
        return wrapper
    return decorator_retry
    

# example

@retry(on=['TimeoutError'], limit=3)
def crawl(url):
    pass
    
INFO