当前位置: 首页>>代码示例>>Python>>正文


Python tenacity.retry_if_exception_type方法代码示例

本文整理汇总了Python中tenacity.retry_if_exception_type方法的典型用法代码示例。如果您正苦于以下问题:Python tenacity.retry_if_exception_type方法的具体用法?Python tenacity.retry_if_exception_type怎么用?Python tenacity.retry_if_exception_type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tenacity的用法示例。


在下文中一共展示了tenacity.retry_if_exception_type方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_retry_on_conn_error

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [as 别名]
def test_retry_on_conn_error(self, mocked_session):

        retry_args = dict(
            wait=tenacity.wait_none(),
            stop=tenacity.stop_after_attempt(7),
            retry=tenacity.retry_if_exception_type(
                requests.exceptions.ConnectionError
            )
        )

        def send_and_raise(unused_request, **kwargs):
            raise requests.exceptions.ConnectionError

        mocked_session().send.side_effect = send_and_raise
        # The job failed for some reason
        with self.assertRaises(tenacity.RetryError):
            self.get_hook.run_with_advanced_retry(
                endpoint='v1/test',
                _retry_args=retry_args
            )
        self.assertEqual(
            self.get_hook._retry_obj.stop.max_attempt_number + 1,
            mocked_session.call_count
        ) 
开发者ID:apache,项目名称:airflow,代码行数:26,代码来源:test_http.py

示例2: test_run_with_advanced_retry

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [as 别名]
def test_run_with_advanced_retry(self, m):

        m.get(
            'http://test:8080/v1/test',
            status_code=200,
            reason='OK'
        )

        retry_args = dict(
            wait=tenacity.wait_none(),
            stop=tenacity.stop_after_attempt(3),
            retry=tenacity.retry_if_exception_type(Exception),
            reraise=True
        )
        with mock.patch(
            'airflow.hooks.base_hook.BaseHook.get_connection',
            side_effect=get_airflow_connection
        ):
            response = self.get_hook.run_with_advanced_retry(
                endpoint='v1/test',
                _retry_args=retry_args
            )
            self.assertIsInstance(response, requests.Response) 
开发者ID:apache,项目名称:airflow,代码行数:25,代码来源:test_http.py

示例3: _get_connection

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [as 别名]
def _get_connection(self):
        """Context manager providing a netmiko SSH connection object.

        This function hides the complexities of gracefully handling retrying
        failed connection attempts.
        """
        retry_exc_types = (paramiko.SSHException, EOFError)

        # Use tenacity to handle retrying.
        @tenacity.retry(
            # Log a message after each failed attempt.
            after=tenacity.after_log(LOG, logging.DEBUG),
            # Reraise exceptions if our final attempt fails.
            reraise=True,
            # Retry on SSH connection errors.
            retry=tenacity.retry_if_exception_type(retry_exc_types),
            # Stop after the configured timeout.
            stop=tenacity.stop_after_delay(
                int(self.ngs_config['ngs_ssh_connect_timeout'])),
            # Wait for the configured interval between attempts.
            wait=tenacity.wait_fixed(
                int(self.ngs_config['ngs_ssh_connect_interval'])),
        )
        def _create_connection():
            return netmiko.ConnectHandler(**self.config)

        # First, create a connection.
        try:
            net_connect = _create_connection()
        except tenacity.RetryError as e:
            LOG.error("Reached maximum SSH connection attempts, not retrying")
            raise exc.GenericSwitchNetmikoConnectError(
                config=device_utils.sanitise_config(self.config), error=e)
        except Exception as e:
            LOG.error("Unexpected exception during SSH connection")
            raise exc.GenericSwitchNetmikoConnectError(
                config=device_utils.sanitise_config(self.config), error=e)

        # Now yield the connection to the caller.
        with net_connect:
            yield net_connect 
开发者ID:openstack,项目名称:networking-generic-switch,代码行数:43,代码来源:__init__.py

示例4: retrying

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [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)
        ) 
开发者ID:socialwifi,项目名称:jsonapi-requests,代码行数:12,代码来源:request_factory.py

示例5: test_context_manager_on_error

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [as 别名]
def test_context_manager_on_error(self):
        from tenacity import Retrying

        class CustomError(Exception):
            pass

        retry = Retrying(retry=tenacity.retry_if_exception_type(IOError))

        def test():
            for attempt in retry:
                with attempt:
                    raise CustomError("Don't retry!")

        self.assertRaises(CustomError, test) 
开发者ID:jd,项目名称:tenacity,代码行数:16,代码来源:test_tenacity.py

示例6: join_group

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [as 别名]
def join_group(self, group_id):
        if (not self._coordinator or not self._coordinator.is_started
                or not group_id):
            return

        @tenacity.retry(
            wait=tenacity.wait_exponential(
                multiplier=self.conf.coordination.retry_backoff,
                max=self.conf.coordination.max_retry_interval),
            retry=tenacity.retry_if_exception_type(
                ErrorJoiningPartitioningGroup))
        def _inner():
            try:
                join_req = self._coordinator.join_group(group_id)
                join_req.get()
                LOG.info('Joined partitioning group %s', group_id)
            except tooz.coordination.MemberAlreadyExist:
                return
            except tooz.coordination.GroupNotCreated:
                create_grp_req = self._coordinator.create_group(group_id)
                try:
                    create_grp_req.get()
                except tooz.coordination.GroupAlreadyExist:
                    pass
                raise ErrorJoiningPartitioningGroup()
            except tooz.coordination.ToozError:
                LOG.exception('Error joining partitioning group %s,'
                              ' re-trying', group_id)
                raise ErrorJoiningPartitioningGroup()
            self._groups.add(group_id)

        return _inner() 
开发者ID:openstack,项目名称:aodh,代码行数:34,代码来源:coordination.py

示例7: test_detach

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [as 别名]
def test_detach(self):
        """Admin only operation."""
        function_id = self.create_function()
        version = self.create_function_version(function_id)

        # Create execution to allocate worker
        resp, _ = self.client.create_execution(
            function_id, input='{"name": "Qinling"}', version=version
        )
        self.assertEqual(201, resp.status)

        resp, body = self.admin_client.get_function_workers(function_id,
                                                            version=version)
        self.assertEqual(200, resp.status)
        self.assertEqual(1, len(body['workers']))

        # Detach function version from workers
        resp, _ = self.admin_client.detach_function(function_id,
                                                    version=version)
        self.assertEqual(202, resp.status)

        def _assert_workers():
            resp, body = self.admin_client.get_function_workers(
                function_id,
                version=version
            )
            self.assertEqual(200, resp.status)
            self.assertEqual(0, len(body['workers']))

        r = tenacity.Retrying(
            wait=tenacity.wait_fixed(1),
            stop=tenacity.stop_after_attempt(5),
            retry=tenacity.retry_if_exception_type(AssertionError)
        )
        r.call(_assert_workers) 
开发者ID:openstack,项目名称:qinling,代码行数:37,代码来源:test_function_versions.py

示例8: test_detach

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [as 别名]
def test_detach(self):
        """Admin only operation."""
        function_id = self.create_function(self.python_zip_file)
        resp, _ = self.client.create_execution(
            function_id, input='{"name": "Qinling"}'
        )
        self.assertEqual(201, resp.status)

        resp, body = self.admin_client.get_function_workers(function_id)
        self.assertEqual(200, resp.status)
        self.assertEqual(1, len(body['workers']))

        # Detach function
        resp, _ = self.admin_client.detach_function(function_id)
        self.assertEqual(202, resp.status)

        def _assert_workers():
            resp, body = self.admin_client.get_function_workers(function_id)
            self.assertEqual(200, resp.status)
            self.assertEqual(0, len(body['workers']))

        r = tenacity.Retrying(
            wait=tenacity.wait_fixed(1),
            stop=tenacity.stop_after_attempt(5),
            retry=tenacity.retry_if_exception_type(AssertionError)
        )
        r.call(_assert_workers) 
开发者ID:openstack,项目名称:qinling,代码行数:29,代码来源:test_functions.py

示例9: test_wait_retry_state_attributes

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [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

示例10: url_request

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception_type [as 别名]
def url_request(request_session, url, body=None):
    """Send request to a service url."""
    exception = None

    # Send ping request first to make sure the url works
    try:
        temp = url.split('/')
        temp[-1] = 'ping'
        ping_url = '/'.join(temp)
        r = tenacity.Retrying(
            wait=tenacity.wait_fixed(1),
            stop=tenacity.stop_after_attempt(30),
            reraise=True,
            retry=tenacity.retry_if_exception_type(IOError)
        )
        r.call(request_session.get, ping_url, timeout=(3, 3), verify=False)
    except Exception as e:
        LOG.exception(
            "Failed to request url %s, error: %s", ping_url, str(e)
        )
        return False, {'output': 'Function execution failed.'}

    for a in six.moves.xrange(10):
        res = None
        try:
            # Default execution max duration is 3min, could be configurable
            res = request_session.post(
                url, json=body, timeout=(3, 180), verify=False
            )
            return True, res.json()
        except requests.ConnectionError as e:
            exception = e
            time.sleep(1)
        except Exception as e:
            LOG.exception(
                "Failed to request url %s, error: %s", url, str(e)
            )
            if res:
                LOG.error("Response status: %s, content: %s",
                          res.status_code, res.content)

            return False, {'output': 'Function execution timeout.'}

    LOG.exception("Could not connect to function service. Reason: %s",
                  exception)

    return False, {'output': 'Internal service error.'} 
开发者ID:openstack,项目名称:qinling,代码行数:49,代码来源:utils.py


注:本文中的tenacity.retry_if_exception_type方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。