Sobes.tech
Junior — Senior

Decorator with automatic retry on exception

livecode

Task condition

Create a decorator that, when an error occurs, will attempt to execute the wrapped function a few more times. The number of attempts is set by a decorator parameter. If the function successfully completes within these attempts, its result should be returned. If after all attempts the error persists, it should be raised further.

Example usage:

@retry(times=3)
def unreliable_func():
    # code that may throw an exception
    ...