本文整理汇总了Python中tenacity.stop_after_delay方法的典型用法代码示例。如果您正苦于以下问题:Python tenacity.stop_after_delay方法的具体用法?Python tenacity.stop_after_delay怎么用?Python tenacity.stop_after_delay使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tenacity
的用法示例。
在下文中一共展示了tenacity.stop_after_delay方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _wait_for_init_container
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [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 stop_after_delay [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: _do_it_with_persistence
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [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()
示例4: post
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [as 别名]
def post(self):
buf = snappy.uncompress(pecan.request.body)
f = remote_pb2.WriteRequest()
f.ParseFromString(buf)
measures_by_rid = collections.defaultdict(dict)
for ts in f.timeseries:
attrs = dict((l.name, l.value) for l in ts.labels)
original_rid = (attrs.get("job", "none"),
attrs.get("instance", "none"))
name = attrs['__name__']
if ts.samples:
data = [{'timestamp': s.timestamp_ms / 1000.0,
'value': s.value} for s in ts.samples]
measures_by_rid[original_rid][name] = validate(
MeasuresListSchema, data)
creator = pecan.request.auth_helper.get_current_user(pecan.request)
measures_to_batch = {}
for (job, instance), measures in measures_by_rid.items():
original_rid = '%s@%s' % (job, instance)
rid = ResourceUUID(original_rid, creator=creator)
metric_names = list(measures.keys())
timeout = pecan.request.conf.api.operation_timeout
metrics = get_or_create_resource_and_metrics.retry_with(
stop=tenacity.stop_after_delay(timeout))(
creator, rid, original_rid, metric_names,
dict(job=job, instance=instance),
"prometheus", self.PROMETHEUS_RESOURCE_TYPE)
for metric in metrics:
enforce("post measures", metric)
measures_to_batch.update(
dict((metric.id, measures[metric.name]) for metric in
metrics if metric.name in measures))
pecan.request.incoming.add_measures_batch(measures_to_batch)
pecan.response.status = 202
示例5: __init__
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [as 别名]
def __init__(self, conf):
super(S3Storage, self).__init__(conf)
self.s3, self._region_name, self._bucket_prefix = (
s3.get_connection(conf)
)
self._bucket_name = '%s-aggregates' % self._bucket_prefix
if conf.s3_check_consistency_timeout > 0:
self._consistency_stop = tenacity.stop_after_delay(
conf.s3_check_consistency_timeout)
else:
self._consistency_stop = None
示例6: __enter__
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [as 别名]
def __enter__(self):
self.lock = False
if not self.coordinator:
return self
LOG.debug("Trying to acquire lock for %s", self.locks_prefix)
names = itertools.cycle(self.lock_names)
retry_kwargs = {'wait': tenacity.wait_random(min=0, max=1),
'reraise': True}
if self.timeout:
retry_kwargs['stop'] = tenacity.stop_after_delay(self.timeout)
@tenacity.retry(**retry_kwargs)
def grab_lock_from_pool():
name = next(names)
# NOTE(pas-ha) currently all tooz backends support locking API.
# In case this changes, this should be wrapped to not respin
# lock grabbing on NotImplemented exception.
lock = self.coordinator.get_lock(name.encode())
locked = lock.acquire(blocking=False)
if not locked:
raise coordination.LockAcquireFailed(
"Failed to acquire lock %s" % name)
return lock
try:
self.lock = grab_lock_from_pool()
except Exception:
msg = ("Failed to acquire any of %s locks for %s "
"for a netmiko action in %s seconds. "
"Try increasing acquire_timeout." % (
self.locks_pool_size, self.locks_prefix,
self.timeout))
LOG.error(msg, exc_info=True)
raise
return self
示例7: _get_connection
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [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
示例8: test_stop_any
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [as 别名]
def test_stop_any(self):
stop = tenacity.stop_any(
tenacity.stop_after_delay(1),
tenacity.stop_after_attempt(4))
def s(*args):
return stop(make_retry_state(*args))
self.assertFalse(s(1, 0.1))
self.assertFalse(s(2, 0.2))
self.assertFalse(s(2, 0.8))
self.assertTrue(s(4, 0.8))
self.assertTrue(s(3, 1.8))
self.assertTrue(s(4, 1.8))
示例9: test_stop_all
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [as 别名]
def test_stop_all(self):
stop = tenacity.stop_all(
tenacity.stop_after_delay(1),
tenacity.stop_after_attempt(4))
def s(*args):
return stop(make_retry_state(*args))
self.assertFalse(s(1, 0.1))
self.assertFalse(s(2, 0.2))
self.assertFalse(s(2, 0.8))
self.assertFalse(s(4, 0.8))
self.assertFalse(s(3, 1.8))
self.assertTrue(s(4, 1.8))
示例10: test_stop_or
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [as 别名]
def test_stop_or(self):
stop = tenacity.stop_after_delay(1) | tenacity.stop_after_attempt(4)
def s(*args):
return stop(make_retry_state(*args))
self.assertFalse(s(1, 0.1))
self.assertFalse(s(2, 0.2))
self.assertFalse(s(2, 0.8))
self.assertTrue(s(4, 0.8))
self.assertTrue(s(3, 1.8))
self.assertTrue(s(4, 1.8))
示例11: test_stop_and
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [as 别名]
def test_stop_and(self):
stop = tenacity.stop_after_delay(1) & tenacity.stop_after_attempt(4)
def s(*args):
return stop(make_retry_state(*args))
self.assertFalse(s(1, 0.1))
self.assertFalse(s(2, 0.2))
self.assertFalse(s(2, 0.8))
self.assertFalse(s(4, 0.8))
self.assertFalse(s(3, 1.8))
self.assertTrue(s(4, 1.8))
示例12: test_stop_after_delay
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_delay [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)))