本文整理匯總了Python中retrying.Retrying方法的典型用法代碼示例。如果您正苦於以下問題:Python retrying.Retrying方法的具體用法?Python retrying.Retrying怎麽用?Python retrying.Retrying使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類retrying
的用法示例。
在下文中一共展示了retrying.Retrying方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: retry_on_exception
# 需要導入模塊: import retrying [as 別名]
# 或者: from retrying import Retrying [as 別名]
def retry_on_exception(*args):
"""Takes a list of exceptions to retry on and retries, will automatically unpack ActivityTaskFailedError exceptions
:param *args: exception classes to retry on
:type *args: Iterable
:rtype: function
:return: callable function to be used with retry_on_exception in Retrying
"""
def _retry_exceptions(exception):
if isinstance(exception, ActivityTaskFailedError):
exception = exception.cause
if isinstance(exception, args):
return True
return False
return _retry_exceptions
示例2: initialized_retry
# 需要導入模塊: import retrying [as 別名]
# 或者: from retrying import Retrying [as 別名]
def initialized_retry(*dargs, **dkw):
def wrap(f):
@wraps(f)
def wrapped_f(*args, **kw):
if _retry_init:
rargs, rkw = _retry_init(dargs, dkw)
else:
rargs, rkw = dargs, dkw
return Retrying(*rargs, **rkw).call(_warn_about_exceptions(f), *args, **kw)
return wrapped_f
return wrap
# for operations that shouldn't take longer than a few seconds (e.g. HTTP request)
# will retry after 2s, 4s, 8s, 10s, 10s, 10s ... until the 10th attempt
示例3: __init__
# 需要導入模塊: import retrying [as 別名]
# 或者: from retrying import Retrying [as 別名]
def __init__(self, stop_max_attempt_number=None, stop_max_delay=None, wait_fixed=None,
wait_random_min=None, wait_random_max=None, wait_incrementing_start=None,
wait_incrementing_increment=None, wait_exponential_multiplier=None, wait_exponential_max=None,
retry_on_exception=None, retry_on_result=None, wrap_exception=False, stop_func=None, wait_func=None):
super(Retrying, self).__init__(stop=stop_func, wait=wait_func, stop_max_attempt_number=stop_max_attempt_number,
stop_max_delay=stop_max_delay, wait_fixed=wait_fixed,
wait_random_min=wait_random_min,
wait_random_max=wait_random_max, wait_incrementing_start=wait_incrementing_start,
wait_incrementing_increment=wait_incrementing_increment,
wait_exponential_multiplier=wait_exponential_multiplier,
wait_exponential_max=wait_exponential_max, retry_on_exception=retry_on_exception,
retry_on_result=retry_on_result,
wrap_exception=wrap_exception,
stop_func=stop_func, wait_func=wait_func)
# unfortunately retrying uses ms everywhere and we are using seconds (as in floats 0.5 is valid)
# to remain consistent with botoflow, we fix all the times before passing the to retrying
self._stop_max_attempt_number = 3 if stop_max_attempt_number is None else stop_max_attempt_number
self._stop_max_delay = 1000 if stop_max_delay is None else int(stop_max_delay * 1000)
self._wait_fixed = 1000 if wait_fixed is None else int(wait_fixed * 1000)
self._wait_random_min = 0 if wait_random_min is None else int(wait_random_min * 1000)
self._wait_random_max = 1000 if wait_random_max is None else int(wait_random_max * 1000)
self._wait_incrementing_start = 0 if wait_incrementing_start is None else int(wait_incrementing_start * 1000)
self._wait_incrementing_increment = (1000 if wait_incrementing_increment is None
else int(wait_incrementing_increment * 1000))
self._wait_exponential_multiplier = (1 if wait_exponential_multiplier is None
else int(wait_exponential_multiplier * 1000))
self._wait_exponential_max = (retrying.MAX_WAIT if wait_exponential_max is None
else int(wait_exponential_max * 1000))
示例4: _warn_about_exceptions
# 需要導入模塊: import retrying [as 別名]
# 或者: from retrying import Retrying [as 別名]
def _warn_about_exceptions(f, *args, **kw):
try:
return f(*args, **kw)
except Exception as e:
logging.warning("Retrying: {} (message was: {})".format(
f.__name__, str(e)))
raise
示例5: retry_generator
# 需要導入模塊: import retrying [as 別名]
# 或者: from retrying import Retrying [as 別名]
def retry_generator(fn=None, max_retries=8, retry_multiplier=5.0, *args, **kwargs):
@decorator
def _decor_(fn, *args, **kwargs):
"""
Retry a generator. The if you don't expect already yielded items to be
yielded again, the generator will need to keep state about what items have
already been successfully yielded.
"""
for retry in range(1, max_retries + 1):
try:
generator = fn(*args, **kwargs)
if not isinstance(generator, GeneratorType):
msg = "@retry_generator cannot be used in non-generator functions"
raise NonGeneratorError(msg)
for i in generator:
yield i
except (StopIteration, NonGeneratorError):
raise
except Exception as e:
if retry < max_retries:
logging.warning("Retrying: {} (message was: {})".format(
fn.__name__, str(e)))
time.sleep(retry * retry_multiplier)
else:
raise
else:
break
return _decor_(fn, *args, **kwargs) if fn is not None else _decor_
示例6: retry
# 需要導入模塊: import retrying [as 別名]
# 或者: from retrying import Retrying [as 別名]
def retry(method_to_check, retry_timeout=RETRY_TIMEOUT,
retry_interval=RETRY_INTERVAL):
return Retrying(stop_max_delay=retry_timeout * 1000,
wait_fixed=retry_interval * 1000).call(method_to_check)
示例7: db_func_with_retry
# 需要導入模塊: import retrying [as 別名]
# 或者: from retrying import Retrying [as 別名]
def db_func_with_retry(db_func, *args, **kwargs):
"""
This method is a generic retry function to support database setup and cleanup.
"""
# Using as an annotation would be nice but annotations are evaluated at import
# time and simple ways to use the annotation means the config gets read before
# it is setup. Likely there is a way to use some proxies to delay the actual
# reading of config values however this is lesser code.
retrying_obj = retrying.Retrying(
retry_on_exception=_retry_if_connection_error,
wait_exponential_multiplier=cfg.CONF.database.connection_retry_backoff_mul * 1000,
wait_exponential_max=cfg.CONF.database.connection_retry_backoff_max_s * 1000,
stop_max_delay=cfg.CONF.database.connection_retry_max_delay_m * 60 * 1000
)
return retrying_obj.call(db_func, *args, **kwargs)
示例8: register_exchanges_with_retry
# 需要導入模塊: import retrying [as 別名]
# 或者: from retrying import Retrying [as 別名]
def register_exchanges_with_retry():
def retry_if_io_error(exception):
return isinstance(exception, socket.error)
retrying_obj = retrying.Retrying(
retry_on_exception=retry_if_io_error,
wait_fixed=cfg.CONF.messaging.connection_retry_wait,
stop_max_attempt_number=cfg.CONF.messaging.connection_retries
)
return retrying_obj.call(register_exchanges)
示例9: _get
# 需要導入模塊: import retrying [as 別名]
# 或者: from retrying import Retrying [as 別名]
def _get(self, url, package):
retry = Retrying(wait_exponential_multiplier=2000, wait_exponential_max=120000,
retry_on_exception=_retry_msg)
return retry.call(requests.get, url % package)