本文整理汇总了Python中tenacity.wait_random方法的典型用法代码示例。如果您正苦于以下问题:Python tenacity.wait_random方法的具体用法?Python tenacity.wait_random怎么用?Python tenacity.wait_random使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tenacity
的用法示例。
在下文中一共展示了tenacity.wait_random方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __enter__
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import wait_random [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
示例2: test_random_sleep
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import wait_random [as 别名]
def test_random_sleep(self):
r = Retrying(wait=tenacity.wait_random(min=1, max=20))
times = set()
for x in six.moves.range(1000):
times.add(r.wait(1, 6546))
# this is kind of non-deterministic...
self.assertTrue(len(times) > 1)
for t in times:
self.assertTrue(t >= 1)
self.assertTrue(t < 20)
示例3: test_random_sleep_without_min
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import wait_random [as 别名]
def test_random_sleep_without_min(self):
r = Retrying(wait=tenacity.wait_random(max=2))
times = set()
times.add(r.wait(1, 6546))
times.add(r.wait(1, 6546))
times.add(r.wait(1, 6546))
times.add(r.wait(1, 6546))
# this is kind of non-deterministic...
self.assertTrue(len(times) > 1)
for t in times:
self.assertTrue(t >= 0)
self.assertTrue(t <= 2)
示例4: test_wait_combine
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import wait_random [as 别名]
def test_wait_combine(self):
r = Retrying(wait=tenacity.wait_combine(tenacity.wait_random(0, 3),
tenacity.wait_fixed(5)))
# Test it a few time since it's random
for i in six.moves.range(1000):
w = r.wait(1, 5)
self.assertLess(w, 8)
self.assertGreaterEqual(w, 5)
示例5: test_wait_double_sum
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import wait_random [as 别名]
def test_wait_double_sum(self):
r = Retrying(wait=tenacity.wait_random(0, 3) + tenacity.wait_fixed(5))
# Test it a few time since it's random
for i in six.moves.range(1000):
w = r.wait(1, 5)
self.assertLess(w, 8)
self.assertGreaterEqual(w, 5)
示例6: test_wait_triple_sum
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import wait_random [as 别名]
def test_wait_triple_sum(self):
r = Retrying(wait=tenacity.wait_fixed(1) + tenacity.wait_random(0, 3) +
tenacity.wait_fixed(5))
# Test it a few time since it's random
for i in six.moves.range(1000):
w = r.wait(1, 5)
self.assertLess(w, 9)
self.assertGreaterEqual(w, 6)