def retry_predicate(target, wait_gen, predicate, max_tries, jitter, on_success, on_backoff, on_giveup, wait_gen_kwargs): success_hdlrs = _ensure_coroutines(_handlers(on_success)) backoff_hdlrs = _ensure_coroutines(_handlers(on_backoff, _log_backoff)) giveup_hdlrs = _ensure_coroutines(_handlers(on_giveup, _log_giveup)) # Easy to implement, please report if you need this. assert not asyncio.iscoroutinefunction(max_tries) assert not asyncio.iscoroutinefunction(jitter) assert asyncio.iscoroutinefunction(target) @functools.wraps(target) @asyncio.coroutine def retry(*args, **kwargs): # change names because python 2.x doesn't have nonlocal max_tries_ = _maybe_call(max_tries) tries = 0 wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 details = (target, args, kwargs, tries) ret = yield from target(*args, **kwargs) if predicate(ret): if tries == max_tries_: yield from _call_handlers(giveup_hdlrs, *details, value=ret) break seconds = _next_wait(wait, jitter) yield from _call_handlers(backoff_hdlrs, *details, value=ret, wait=seconds) # Note: there is no convenient way to pass explicit event # loop to decorator, so here we assume that either default # thread event loop is set and correct (it mostly is # by default), or Python >= 3.5.3 or Python >= 3.6 is used # where loop.get_event_loop() in coroutine guaranteed to # return correct value. # See for details: # <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk> # <https://bugs.python.org/issue28613> yield from asyncio.sleep(seconds) continue else: yield from _call_handlers(success_hdlrs, *details, value=ret) break return ret return retry
def retry_exception(target, wait_gen, exception, max_tries, max_time, jitter, giveup, on_success, on_backoff, on_giveup, wait_gen_kwargs): success_hdlrs = _ensure_coroutines(_handlers(on_success)) backoff_hdlrs = _ensure_coroutines(_handlers(on_backoff, _log_backoff)) giveup_hdlrs = _ensure_coroutines(_handlers(on_giveup, _log_giveup)) giveup = _ensure_coroutine(giveup) # Easy to implement, please report if you need this. assert not asyncio.iscoroutinefunction(max_tries) assert not asyncio.iscoroutinefunction(jitter) @functools.wraps(target) async def retry(*args, **kwargs): # change names because python 2.x doesn't have nonlocal max_tries_ = _maybe_call(max_tries) max_time_ = _maybe_call(max_time) tries = 0 start = datetime.datetime.now() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 elapsed = timedelta.total_seconds(datetime.datetime.now() - start) details = (target, args, kwargs, tries, elapsed) try: ret = await target(*args, **kwargs) except exception as e: giveup_result = await giveup(e) max_tries_exceeded = (tries == max_tries_) max_time_exceeded = (max_time_ is not None and elapsed >= max_time_) if giveup_result or max_tries_exceeded or max_time_exceeded: await _call_handlers(giveup_hdlrs, *details) raise seconds = _next_wait(wait, jitter, elapsed, max_time_) await _call_handlers(backoff_hdlrs, *details, wait=seconds) # Note: there is no convenient way to pass explicit event # loop to decorator, so here we assume that either default # thread event loop is set and correct (it mostly is # by default), or Python >= 3.5.3 or Python >= 3.6 is used # where loop.get_event_loop() in coroutine guaranteed to # return correct value. # See for details: # <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk> # <https://bugs.python.org/issue28613> await asyncio.sleep(seconds) else: await _call_handlers(success_hdlrs, *details) return ret return retry
def retry_predicate(target, wait_gen, predicate, max_tries, max_time, jitter, on_success, on_backoff, on_giveup, wait_gen_kwargs): success_hdlrs = _handlers(on_success) backoff_hdlrs = _handlers(on_backoff, _log_backoff) giveup_hdlrs = _handlers(on_giveup, _log_giveup) @functools.wraps(target) def retry(*args, **kwargs): # change names because python 2.x doesn't have nonlocal max_tries_ = _maybe_call(max_tries) max_time_ = _maybe_call(max_time) tries = 0 start = datetime.datetime.now() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 elapsed = timedelta.total_seconds(datetime.datetime.now() - start) details = (target, args, kwargs, tries, elapsed) ret = target(*args, **kwargs) if predicate(ret): max_tries_exceeded = (tries == max_tries_) max_time_exceeded = (max_time_ is not None and elapsed >= max_time_) if max_tries_exceeded or max_time_exceeded: _call_handlers(giveup_hdlrs, *details, value=ret) break seconds = _next_wait(wait, jitter, elapsed, max_time_) _call_handlers(backoff_hdlrs, *details, value=ret, wait=seconds) time.sleep(seconds) continue else: _call_handlers(success_hdlrs, *details, value=ret) break return ret return retry
def retry_exception(target, wait_gen, exception, max_tries, max_time, jitter, giveup, on_success, on_backoff, on_giveup, wait_gen_kwargs): success_hdlrs = _handlers(on_success) backoff_hdlrs = _handlers(on_backoff, _log_backoff) giveup_hdlrs = _handlers(on_giveup, _log_giveup) @functools.wraps(target) def retry(*args, **kwargs): # change names because python 2.x doesn't have nonlocal max_tries_ = _maybe_call(max_tries) max_time_ = _maybe_call(max_time) tries = 0 start = datetime.datetime.now() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 elapsed = _total_seconds(datetime.datetime.now() - start) details = (target, args, kwargs, tries, elapsed) try: ret = target(*args, **kwargs) except exception as e: max_tries_exceeded = (tries == max_tries_) max_time_exceeded = (max_time_ is not None and elapsed >= max_time_) if giveup(e) or max_tries_exceeded or max_time_exceeded: _call_handlers(giveup_hdlrs, *details) raise seconds = _next_wait(wait, jitter, elapsed, max_time_) _call_handlers(backoff_hdlrs, *details, wait=seconds) time.sleep(seconds) else: _call_handlers(success_hdlrs, *details) return ret return retry
def retry_predicate(target, wait_gen, predicate, max_tries, jitter, on_success, on_backoff, on_giveup, wait_gen_kwargs): success_hdlrs = _handlers(on_success) backoff_hdlrs = _handlers(on_backoff, _log_backoff) giveup_hdlrs = _handlers(on_giveup, _log_giveup) @functools.wraps(target) def retry(*args, **kwargs): # change names because python 2.x doesn't have nonlocal max_tries_ = _maybe_call(max_tries) tries = 0 wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 details = (target, args, kwargs, tries) ret = target(*args, **kwargs) if predicate(ret): if tries == max_tries_: _call_handlers(giveup_hdlrs, *details, value=ret) break seconds = _next_wait(wait, jitter) _call_handlers(backoff_hdlrs, *details, value=ret, wait=seconds) time.sleep(seconds) continue else: _call_handlers(success_hdlrs, *details, value=ret) break return ret return retry
def retry_exception(target, wait_gen, exception, max_tries, jitter, giveup, on_success, on_backoff, on_giveup, wait_gen_kwargs): success_hdlrs = _handlers(on_success) backoff_hdlrs = _handlers(on_backoff, _log_backoff) giveup_hdlrs = _handlers(on_giveup, _log_giveup) @functools.wraps(target) def retry(*args, **kwargs): # change names because python 2.x doesn't have nonlocal max_tries_ = _maybe_call(max_tries) tries = 0 wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 details = (target, args, kwargs, tries) try: ret = target(*args, **kwargs) except exception as e: if giveup(e) or tries == max_tries_: _call_handlers(giveup_hdlrs, *details) raise seconds = _next_wait(wait, jitter) _call_handlers(backoff_hdlrs, *details, wait=seconds) time.sleep(seconds) else: _call_handlers(success_hdlrs, *details) return ret return retry