本文整理汇总了Python中tenacity.retry_if_result方法的典型用法代码示例。如果您正苦于以下问题:Python tenacity.retry_if_result方法的具体用法?Python tenacity.retry_if_result怎么用?Python tenacity.retry_if_result使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tenacity
的用法示例。
在下文中一共展示了tenacity.retry_if_result方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _wait_for_init_container
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [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 retry_if_result [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: test_wait_chain_multiple_invocations
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [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[:] = []
示例4: test_before_sleep_log_returns
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [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)
示例5: _put_object_safe
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [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)
示例6: test_retry_if_result
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [as 别名]
def test_retry_if_result(self):
retry = (tenacity.retry_if_result(lambda x: x == 1))
def r(fut):
retry_state = make_retry_state(1, 1.0, last_result=fut)
return retry(retry_state)
self.assertTrue(r(tenacity.Future.construct(1, 1, False)))
self.assertFalse(r(tenacity.Future.construct(1, 2, False)))
示例7: test_retry_any
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [as 别名]
def test_retry_any(self):
retry = tenacity.retry_any(
tenacity.retry_if_result(lambda x: x == 1),
tenacity.retry_if_result(lambda x: x == 2))
def r(fut):
retry_state = make_retry_state(1, 1.0, last_result=fut)
return retry(retry_state)
self.assertTrue(r(tenacity.Future.construct(1, 1, False)))
self.assertTrue(r(tenacity.Future.construct(1, 2, False)))
self.assertFalse(r(tenacity.Future.construct(1, 3, False)))
self.assertFalse(r(tenacity.Future.construct(1, 1, True)))
示例8: test_retry_all
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [as 别名]
def test_retry_all(self):
retry = tenacity.retry_all(
tenacity.retry_if_result(lambda x: x == 1),
tenacity.retry_if_result(lambda x: isinstance(x, int)))
def r(fut):
retry_state = make_retry_state(1, 1.0, last_result=fut)
return retry(retry_state)
self.assertTrue(r(tenacity.Future.construct(1, 1, False)))
self.assertFalse(r(tenacity.Future.construct(1, 2, False)))
self.assertFalse(r(tenacity.Future.construct(1, 3, False)))
self.assertFalse(r(tenacity.Future.construct(1, 1, True)))
示例9: test_retry_or
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [as 别名]
def test_retry_or(self):
retry = (tenacity.retry_if_result(lambda x: x == "foo") |
tenacity.retry_if_result(lambda x: isinstance(x, int)))
def r(fut):
retry_state = make_retry_state(1, 1.0, last_result=fut)
return retry(retry_state)
self.assertTrue(r(tenacity.Future.construct(1, "foo", False)))
self.assertFalse(r(tenacity.Future.construct(1, "foobar", False)))
self.assertFalse(r(tenacity.Future.construct(1, 2.2, False)))
self.assertFalse(r(tenacity.Future.construct(1, 42, True)))
示例10: run_health_check
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [as 别名]
def run_health_check(self, ctx, node):
"""Routine to check a node status from a url and recovery if necessary
:param node: The node to be checked.
:returns: True if node is healthy. False otherwise.
"""
max_unhealthy_retry = self.params['poll_url_retry_limit']
retry_interval = self.params['poll_url_retry_interval']
def _return_last_value(retry_state):
return retry_state.outcome.result()
@tenacity.retry(
retry=tenacity.retry_if_result(lambda x: x is False),
wait=tenacity.wait_fixed(retry_interval),
retry_error_callback=_return_last_value,
stop=tenacity.stop_after_attempt(max_unhealthy_retry)
)
def _poll_url_with_retry(url):
return self._poll_url(url, node)
try:
if node.status != consts.NS_ACTIVE:
LOG.info("%s for %s: node is not in ACTIVE state, so skip "
"poll url",
consts.POLL_URL_PASS, node.name)
return True
url_template = self.params['poll_url']
url = self._expand_url_template(url_template, node)
# If health check returns True, return True to mark node as
# healthy. Else return True to mark node as healthy if we are still
# within the node's grace period to allow the node to warm-up.
# Return False to mark the node as unhealthy if we are outside the
# grace period.
return (_poll_url_with_retry(url) or
self._node_within_grace_period(node))
except Exception as ex:
LOG.warning(
"%s for %s: Ignoring error on poll URL: %s",
consts.POLL_URL_PASS, node.name, ex
)
# treat node as healthy when an exception is encountered
return True
示例11: test_wait_retry_state_attributes
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_result [as 别名]
def test_wait_retry_state_attributes(self):
class ExtractCallState(Exception):
pass
# retry_state is mutable, so return it as an exception to extract the
# exact values it has when wait is called and bypass any other logic.
def waitfunc(retry_state):
raise ExtractCallState(retry_state)
retrying = Retrying(
wait=waitfunc,
retry=(tenacity.retry_if_exception_type() |
tenacity.retry_if_result(lambda result: result == 123)))
def returnval():
return 123
try:
retrying.call(returnval)
except ExtractCallState as err:
retry_state = err.args[0]
self.assertIs(retry_state.fn, returnval)
self.assertEqual(retry_state.args, ())
self.assertEqual(retry_state.kwargs, {})
self.assertEqual(retry_state.outcome.result(), 123)
self.assertEqual(retry_state.attempt_number, 1)
self.assertGreater(retry_state.outcome_timestamp,
retry_state.start_time)
def dying():
raise Exception("Broken")
try:
retrying.call(dying)
except ExtractCallState as err:
retry_state = err.args[0]
self.assertIs(retry_state.fn, dying)
self.assertEqual(retry_state.args, ())
self.assertEqual(retry_state.kwargs, {})
self.assertEqual(str(retry_state.outcome.exception()), 'Broken')
self.assertEqual(retry_state.attempt_number, 1)
self.assertGreater(retry_state.outcome_timestamp,
retry_state.start_time)