本文整理汇总了Python中tenacity.Retrying方法的典型用法代码示例。如果您正苦于以下问题:Python tenacity.Retrying方法的具体用法?Python tenacity.Retrying怎么用?Python tenacity.Retrying使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tenacity
的用法示例。
在下文中一共展示了tenacity.Retrying方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _wait_for_init_container
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def _wait_for_init_container(self, context, container, timeout=3600):
def retry_if_result_is_false(result):
return result is False
def check_init_container_stopped():
status = self.show(context, container).status
if status == consts.STOPPED:
return True
elif status == consts.RUNNING:
return False
else:
raise exception.ZunException(
_("Container has unexpected status: %s") % status)
r = tenacity.Retrying(
stop=tenacity.stop_after_delay(timeout),
wait=tenacity.wait_exponential(),
retry=tenacity.retry_if_result(retry_if_result_is_false))
r.call(check_init_container_stopped)
示例2: _wait_for_init_container
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def _wait_for_init_container(self, context, container, timeout=3600):
def retry_if_result_is_false(result):
return result is False
def check_init_container_stopped():
status = self._show_container(context, container).status
if status == consts.STOPPED:
return True
elif status == consts.RUNNING:
return False
else:
raise exception.ZunException(
_("Container has unexpected status: %s") % status)
r = tenacity.Retrying(
stop=tenacity.stop_after_delay(timeout),
wait=tenacity.wait_exponential(),
retry=tenacity.retry_if_result(retry_if_result_is_false))
r.call(check_init_container_stopped)
示例3: retry_api_call
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def retry_api_call(func, config, logger=None, *args, **kwargs):
retry = tenacity.Retrying(
retry=retry_if_exception(
lambda e: getattr(e, "response", {}).get("Error", {}).get("Code", None)
in config.exceptions
if e
else False
),
stop=stop_after_attempt(config.attempt),
wait=wait_exponential(
multiplier=config.multiplier,
max=config.max_delay,
exp_base=config.exponential_base,
),
after=after_log(logger, logger.level) if logger else None,
reraise=True,
)
return retry(func, *args, **kwargs)
示例4: test_wait_backward_compat_with_result
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def test_wait_backward_compat_with_result(self):
captures = []
def wait_capture(attempt, delay, last_result=None):
captures.append(last_result)
return 1
def dying():
raise Exception("Broken")
r_attempts = 10
r = Retrying(wait=wait_capture, sleep=lambda secs: None,
stop=tenacity.stop_after_attempt(r_attempts),
reraise=True)
with reports_deprecation_warning():
self.assertRaises(Exception, r.call, dying)
self.assertEqual(r_attempts - 1, len(captures))
self.assertTrue(all([r.failed for r in captures]))
示例5: test_wait_chain_multiple_invocations
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def test_wait_chain_multiple_invocations(self):
sleep_intervals = []
r = Retrying(
sleep=sleep_intervals.append,
wait=tenacity.wait_chain(*[
tenacity.wait_fixed(i + 1) for i in six.moves.range(3)
]),
stop=tenacity.stop_after_attempt(5),
retry=tenacity.retry_if_result(lambda x: x == 1),
)
@r.wraps
def always_return_1():
return 1
self.assertRaises(tenacity.RetryError, always_return_1)
self.assertEqual(sleep_intervals, [1.0, 2.0, 3.0, 3.0])
sleep_intervals[:] = []
# Clear and restart retrying.
self.assertRaises(tenacity.RetryError, always_return_1)
self.assertEqual(sleep_intervals, [1.0, 2.0, 3.0, 3.0])
sleep_intervals[:] = []
示例6: test_retry_child_class_with_override_backward_compat
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def test_retry_child_class_with_override_backward_compat(self):
def always_true(_):
return True
class MyRetry(tenacity.retry_if_exception):
def __init__(self):
super(MyRetry, self).__init__(always_true)
def __call__(self, attempt):
return super(MyRetry, self).__call__(attempt)
retrying = Retrying(wait=tenacity.wait_fixed(0.01),
stop=tenacity.stop_after_attempt(1),
retry=MyRetry())
def failing():
raise NotImplementedError()
with pytest.raises(RetryError):
retrying.call(failing)
示例7: test_before_sleep_log_raises
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def test_before_sleep_log_raises(self):
thing = NoIOErrorAfterCount(2)
logger = logging.getLogger(self.id())
logger.propagate = False
logger.setLevel(logging.INFO)
handler = CapturingHandler()
logger.addHandler(handler)
try:
_before_sleep = tenacity.before_sleep_log(logger, logging.INFO)
retrying = Retrying(wait=tenacity.wait_fixed(0.01),
stop=tenacity.stop_after_attempt(3),
before_sleep=_before_sleep)
retrying.call(thing.go)
finally:
logger.removeHandler(handler)
etalon_re = (r"^Retrying .* in 0\.01 seconds as it raised "
r"(IO|OS)Error: Hi there, I'm an IOError\.$")
self.assertEqual(len(handler.records), 2)
fmt = logging.Formatter().format
self.assertRegexpMatches(fmt(handler.records[0]), etalon_re)
self.assertRegexpMatches(fmt(handler.records[1]), etalon_re)
示例8: test_before_sleep_log_returns
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def test_before_sleep_log_returns(self, exc_info=False):
thing = NoneReturnUntilAfterCount(2)
logger = logging.getLogger(self.id())
logger.propagate = False
logger.setLevel(logging.INFO)
handler = CapturingHandler()
logger.addHandler(handler)
try:
_before_sleep = tenacity.before_sleep_log(logger,
logging.INFO,
exc_info=exc_info)
_retry = tenacity.retry_if_result(lambda result: result is None)
retrying = Retrying(wait=tenacity.wait_fixed(0.01),
stop=tenacity.stop_after_attempt(3),
retry=_retry, before_sleep=_before_sleep)
retrying.call(thing.go)
finally:
logger.removeHandler(handler)
etalon_re = r'^Retrying .* in 0\.01 seconds as it returned None\.$'
self.assertEqual(len(handler.records), 2)
fmt = logging.Formatter().format
self.assertRegexpMatches(fmt(handler.records[0]), etalon_re)
self.assertRegexpMatches(fmt(handler.records[1]), etalon_re)
示例9: retry_on_exception_and_log
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def retry_on_exception_and_log(msg):
return tenacity.Retrying(
wait=wait_exponential, retry=_retry_on_exception_and_log(msg)).wraps
示例10: _put_object_safe
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def _put_object_safe(self, Bucket, Key, Body):
put = self.s3.put_object(Bucket=Bucket, Key=Key, Body=Body)
if self._consistency_stop:
def _head():
return self.s3.head_object(Bucket=Bucket,
Key=Key, IfMatch=put['ETag'])
tenacity.Retrying(
retry=tenacity.retry_if_result(
lambda r: r['ETag'] != put['ETag']),
wait=self._consistency_wait,
stop=self._consistency_stop)(_head)
示例11: run_with_advanced_retry
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def run_with_advanced_retry(self, _retry_args, *args, **kwargs):
"""
Runs Hook.run() with a Tenacity decorator attached to it. This is useful for
connectors which might be disturbed by intermittent issues and should not
instantly fail.
:param _retry_args: Arguments which define the retry behaviour.
See Tenacity documentation at https://github.com/jd/tenacity
:type _retry_args: dict
.. code-block:: python
hook = HttpHook(http_conn_id='my_conn',method='GET')
retry_args = dict(
wait=tenacity.wait_exponential(),
stop=tenacity.stop_after_attempt(10),
retry=requests.exceptions.ConnectionError
)
hook.run_with_advanced_retry(
endpoint='v1/test',
_retry_args=retry_args
)
"""
self._retry_obj = tenacity.Retrying(
**_retry_args
)
return self._retry_obj(self.run, *args, **kwargs)
示例12: retrying
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def retrying(self):
retry_condition = (
tenacity.retry_if_exception_type(ApiConnectionError)
| tenacity.retry_if_exception_type(ApiInternalServerError)
)
return tenacity.Retrying(
reraise=True,
retry=retry_condition,
stop=tenacity.stop_after_attempt(self.config.RETRIES)
)
示例13: test_never_stop
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def test_never_stop(self):
r = Retrying()
self.assertFalse(r.stop(make_retry_state(3, 6546)))
示例14: test_stop_after_attempt
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def test_stop_after_attempt(self):
r = Retrying(stop=tenacity.stop_after_attempt(3))
self.assertFalse(r.stop(make_retry_state(2, 6546)))
self.assertTrue(r.stop(make_retry_state(3, 6546)))
self.assertTrue(r.stop(make_retry_state(4, 6546)))
示例15: test_stop_after_delay
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import Retrying [as 别名]
def test_stop_after_delay(self):
r = Retrying(stop=tenacity.stop_after_delay(1))
self.assertFalse(r.stop(make_retry_state(2, 0.999)))
self.assertTrue(r.stop(make_retry_state(2, 1)))
self.assertTrue(r.stop(make_retry_state(2, 1.001)))