當前位置: 首頁>>代碼示例>>Python>>正文


Python tenacity.retry_if_result方法代碼示例

本文整理匯總了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) 
開發者ID:openstack,項目名稱:zun,代碼行數:21,代碼來源:driver.py

示例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) 
開發者ID:openstack,項目名稱:zun,代碼行數:21,代碼來源:driver.py

示例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[:] = [] 
開發者ID:jd,項目名稱:tenacity,代碼行數:25,代碼來源:test_tenacity.py

示例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) 
開發者ID:jd,項目名稱:tenacity,代碼行數:26,代碼來源:test_tenacity.py

示例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) 
開發者ID:gnocchixyz,項目名稱:gnocchi,代碼行數:16,代碼來源:s3.py

示例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))) 
開發者ID:jd,項目名稱:tenacity,代碼行數:10,代碼來源:test_tenacity.py

示例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))) 
開發者ID:jd,項目名稱:tenacity,代碼行數:14,代碼來源:test_tenacity.py

示例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))) 
開發者ID:jd,項目名稱:tenacity,代碼行數:14,代碼來源:test_tenacity.py

示例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))) 
開發者ID:jd,項目名稱:tenacity,代碼行數:13,代碼來源:test_tenacity.py

示例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 
開發者ID:openstack,項目名稱:senlin,代碼行數:50,代碼來源:health_manager.py

示例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) 
開發者ID:jd,項目名稱:tenacity,代碼行數:44,代碼來源:test_tenacity.py


注:本文中的tenacity.retry_if_result方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。