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


Python tenacity.retry_if_exception方法代码示例

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


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

示例1: retry_api_call

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception [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) 
开发者ID:laughingman7743,项目名称:PyAthena,代码行数:20,代码来源:util.py

示例2: test_retry_child_class_with_override_backward_compat

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

示例3: _do_it_with_persistence

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception [as 别名]
def _do_it_with_persistence(func, args, config):
    """Exec func with retries based on provided cli flags

    :param: func: function to attempt to execute
    :param: args: argparser generated cli arguments
    :param: config: configparser object of vaultlocker config
    """
    @tenacity.retry(
        wait=tenacity.wait_fixed(1),
        reraise=True,
        stop=(
            tenacity.stop_after_delay(args.retry) if args.retry > 0
            else tenacity.stop_after_attempt(1)
            ),
        retry=(
            tenacity.retry_if_exception(hvac.exceptions.VaultNotInitialized) |
            tenacity.retry_if_exception(hvac.exceptions.VaultDown)
            )
        )
    def _do_it():
        client = _vault_client(config)
        func(args, client, config)
    _do_it() 
开发者ID:openstack-charmers,项目名称:vaultlocker,代码行数:25,代码来源:shell.py

示例4: validate

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception [as 别名]
def validate(self, max_retries=0):
        """Performs basic **connection** validation of a sqlalchemy engine."""

        def _retry_on_exception(exc):
            LOG.warning("Engine connection (validate) failed due to '%s'", exc)
            if isinstance(exc, sa_exc.OperationalError) and \
               _is_db_connection_error(six.text_type(exc.args[0])):
                # We may be able to fix this by retrying...
                return True
            if isinstance(exc, (sa_exc.TimeoutError,
                                sa_exc.ResourceClosedError,
                                sa_exc.DisconnectionError)):
                # We may be able to fix this by retrying...
                return True
            # Other failures we likely can't fix by retrying...
            return False

        @tenacity.retry(
            stop=tenacity.stop_after_attempt(max(0, int(max_retries))),
            wait=tenacity.wait_exponential(),
            reraise=True,
            retry=tenacity.retry_if_exception(_retry_on_exception)
        )
        def _try_connect(engine):
            # See if we can make a connection happen.
            #
            # NOTE(harlowja): note that even though we are connecting
            # once it does not mean that we will be able to connect in
            # the future, so this is more of a sanity test and is not
            # complete connection insurance.
            with contextlib.closing(engine.connect()):
                pass

        _try_connect(self._engine) 
开发者ID:openstack,项目名称:taskflow,代码行数:36,代码来源:impl_sqlalchemy.py

示例5: get_columns

# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import retry_if_exception [as 别名]
def get_columns(self, connection, table_name, schema=None, **kw):
        raw_connection = self._raw_connection(connection)
        schema = schema if schema else raw_connection.schema_name
        query = """
                SELECT
                  table_schema,
                  table_name,
                  column_name,
                  data_type,
                  is_nullable,
                  column_default,
                  ordinal_position,
                  comment
                FROM information_schema.columns
                WHERE table_schema = '{schema}'
                AND table_name = '{table}'
                """.format(
            schema=schema, table=table_name
        )
        retry_config = raw_connection.retry_config
        retry = tenacity.Retrying(
            retry=retry_if_exception(
                lambda exc: self._retry_if_data_catalog_exception(
                    exc, schema, table_name
                )
            ),
            stop=stop_after_attempt(retry_config.attempt),
            wait=wait_exponential(
                multiplier=retry_config.multiplier,
                max=retry_config.max_delay,
                exp_base=retry_config.exponential_base,
            ),
            reraise=True,
        )
        try:
            return [
                {
                    "name": row.column_name,
                    "type": _TYPE_MAPPINGS.get(
                        self._get_column_type(row.data_type), NULLTYPE
                    ),
                    "nullable": True if row.is_nullable == "YES" else False,
                    "default": row.column_default
                    if not self._is_nan(row.column_default)
                    else None,
                    "ordinal_position": row.ordinal_position,
                    "comment": row.comment,
                }
                for row in retry(connection.execute, query).fetchall()
            ]
        except OperationalError as e:
            if not self._retry_if_data_catalog_exception(e, schema, table_name):
                raise_from(NoSuchTableError(table_name), e)
            else:
                raise e 
开发者ID:laughingman7743,项目名称:PyAthena,代码行数:57,代码来源:sqlalchemy_athena.py


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