def retry(exception_processor=generic_exception_processor, max_retries=<NUM_LIT:100>):
max_retries = int(os.getenv('<STR_LIT>', max_retries))<EOL>def yield_new_function_from(f):<EOL><INDENT>def shim(*args, **kwargs):<EOL><INDENT>exc_processor_cxt = None<EOL>retries = <NUM_LIT:0><EOL>while True:<EOL><INDENT>gevent.sleep(<NUM_LIT:0.1>)<EOL>try:<EOL><INDENT>return f(*args, **kwargs)<EOL><DEDENT>except KeyboardInterrupt:<EOL><INDENT>raise<EOL><DEDENT>except Exception:<EOL><INDENT>exception_info_tuple = None<EOL>retries += <NUM_LIT:1><EOL>if max_retries >= <NUM_LIT:1> and retries >= max_retries:<EOL><INDENT>raise<EOL><DEDENT>try:<EOL><INDENT>exception_info_tuple = sys.exc_info()<EOL>exc_processor_cxt = exception_processor(<EOL>exception_info_tuple,<EOL>exc_processor_cxt=exc_processor_cxt)<EOL><DEDENT>finally:<EOL><INDENT>del exception_info_tuple<EOL><DEDENT>duration = min(<NUM_LIT>, (<NUM_LIT:2> ** retries)) / <NUM_LIT:2><EOL>gevent.sleep(duration + random.randint(<NUM_LIT:0>, duration))<EOL><DEDENT><DEDENT><DEDENT>return functools.wraps(f)(shim)<EOL><DEDENT>return yield_new_function_from<EOL>
Generic retry decorator

Tries to call the decorated function.  Should no exception be
raised, the value is simply returned, otherwise, call an
exception_processor function with the exception (type, value,
traceback) tuple (with the intention that it could raise the
exception without losing the traceback) and the exception
processor's optionally usable context value (exc_processor_cxt).

It's recommended to delete all references to the traceback passed
to the exception_processor to speed up garbage collector via the
'del' operator.

This context value is passed to and returned from every invocation
of the exception processor.  This can be used to more conveniently
(vs. an object with __call__ defined) implement exception
processors that have some state, such as the 'number of attempts'.
The first invocation will pass None.

:param f: A function to be retried.
:type f: function

:param exception_processor: A function to process raised
                            exceptions.
:type exception_processor: function

:param max_retries: An integer representing the maximum
                    number of retry attempts.
:type max_retries: integer