本文整理汇总了Python中backoff.on_predicate方法的典型用法代码示例。如果您正苦于以下问题:Python backoff.on_predicate方法的具体用法?Python backoff.on_predicate怎么用?Python backoff.on_predicate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类backoff
的用法示例。
在下文中一共展示了backoff.on_predicate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_on_exception_coro_cancelling
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_exception_coro_cancelling(event_loop):
sleep_started_event = asyncio.Event()
@backoff.on_predicate(backoff.expo)
async def coro():
sleep_started_event.set()
try:
await asyncio.sleep(10)
except asyncio.CancelledError:
return True
return False
task = event_loop.create_task(coro())
await sleep_started_event.wait()
task.cancel()
assert (await task)
示例2: test_on_predicate_on_regular_function_without_event_loop
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate_on_regular_function_without_event_loop(monkeypatch):
monkeypatch.setattr('time.sleep', lambda x: None)
# Set default event loop to None.
loop = asyncio.get_event_loop()
asyncio.set_event_loop(None)
try:
@backoff.on_predicate(backoff.expo)
def return_true(log, n):
val = (len(log) == n - 1)
log.append(val)
return val
log = []
ret = return_true(log, 3)
assert ret is True
assert 3 == len(log)
finally:
# Restore event loop.
asyncio.set_event_loop(loop)
示例3: check_query_status
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def check_query_status(self, execution_id):
"""Check in on the running query, back off if the job is running or queued
Args:
query_execution_id (str): Athena query execution ID
Returns:
bool: True if the query state is SUCCEEDED, False otherwise
Reference https://bit.ly/2uuRtda.
Raises:
AthenaQueryExecutionError: If any failure occurs while checking the status of the
query, this exception will be raised
"""
LOGGER.debug('Checking status of query with execution ID: %s', execution_id)
states_to_backoff = {'QUEUED', 'RUNNING'}
@backoff.on_predicate(backoff.fibo,
lambda resp: \
resp['QueryExecution']['Status']['State'] in states_to_backoff,
max_value=10,
jitter=backoff.full_jitter,
on_backoff=backoff_handler(),
on_success=success_handler(True))
def _check_status(query_execution_id):
return self._client.get_query_execution(
QueryExecutionId=query_execution_id
)
execution_result = _check_status(execution_id)
state = execution_result['QueryExecution']['Status']['State']
if state == 'SUCCEEDED':
return
# When the state is not SUCCEEDED, something bad must have occurred, so raise an exception
reason = execution_result['QueryExecution']['Status']['StateChangeReason']
raise AthenaQueryExecutionError(
'Query \'{}\' {} with reason \'{}\', exiting'.format(execution_id, state, reason)
)
示例4: _try_get_ingress_url
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def _try_get_ingress_url(self, api: kubernetes.client.CoreV1Api) -> str:
"""Return Ingress url when service is ready."""
items = api.list_service_for_all_namespaces().items
for item in items:
ingress = item.status.load_balancer.ingress
if ingress:
return 'http://{}/'.format(ingress[0].hostname or ingress[0].ip)
# @backoff.on_predicate(backoff.constant) will keep running this method
# until it gets a non-falsey result. Return value of '' means that the
# service is not ready yet.
return ''
示例5: _try_get_ready_replicas
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def _try_get_ready_replicas(self,
api: kubernetes.client.ExtensionsV1beta1Api,
label_selector: str) -> int:
"""Return ready replicas when deployment is ready."""
items = api.list_deployment_for_all_namespaces(
label_selector=label_selector).items
for item in items:
if item.status.ready_replicas:
return item.status.ready_replicas
# @backoff.on_predicate(backoff.constant) will keep running this method
# until it gets a non-falsey result. Return value of 0 means that the
# deployment is not ready yet.
return 0
示例6: test_on_predicate
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate(monkeypatch):
monkeypatch.setattr('asyncio.sleep', _await_none)
@backoff.on_predicate(backoff.expo)
async def return_true(log, n):
val = (len(log) == n - 1)
log.append(val)
return val
log = []
ret = await return_true(log, 3)
assert ret is True
assert 3 == len(log)
示例7: test_on_predicate_max_tries
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate_max_tries(monkeypatch):
monkeypatch.setattr('asyncio.sleep', _await_none)
@backoff.on_predicate(backoff.expo, jitter=None, max_tries=3)
async def return_true(log, n):
val = (len(log) == n)
log.append(val)
return val
log = []
ret = await return_true(log, 10)
assert ret is False
assert 3 == len(log)
示例8: test_on_predicate_success
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate_success():
log, log_success, log_backoff, log_giveup = _log_hdlrs()
@backoff.on_predicate(backoff.constant,
on_success=log_success,
on_backoff=log_backoff,
on_giveup=log_giveup,
jitter=None,
interval=0)
@_save_target
async def success(*args, **kwargs):
# succeed after we've backed off twice
return len(log['backoff']) == 2
await success(1, 2, 3, foo=1, bar=2)
# we try 3 times, backing off twice before succeeding
assert len(log['success']) == 1
assert len(log['backoff']) == 2
assert len(log['giveup']) == 0
for i in range(2):
details = log['backoff'][i]
elapsed = details.pop('elapsed')
assert isinstance(elapsed, float)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': success._target,
'tries': i + 1,
'value': False,
'wait': 0}
details = log['success'][0]
elapsed = details.pop('elapsed')
assert isinstance(elapsed, float)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': success._target,
'tries': 3,
'value': True}
示例9: test_on_predicate_giveup
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate_giveup():
log, log_success, log_backoff, log_giveup = _log_hdlrs()
@backoff.on_predicate(backoff.constant,
on_success=log_success,
on_backoff=log_backoff,
on_giveup=log_giveup,
max_tries=3,
jitter=None,
interval=0)
@_save_target
async def emptiness(*args, **kwargs):
pass
await emptiness(1, 2, 3, foo=1, bar=2)
# we try 3 times, backing off twice and giving up once
assert len(log['success']) == 0
assert len(log['backoff']) == 2
assert len(log['giveup']) == 1
details = log['giveup'][0]
elapsed = details.pop('elapsed')
assert isinstance(elapsed, float)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': emptiness._target,
'tries': 3,
'value': None}
示例10: test_on_predicate_iterable_handlers
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate_iterable_handlers():
hdlrs = [_log_hdlrs() for _ in range(3)]
@backoff.on_predicate(backoff.constant,
on_success=(h[1] for h in hdlrs),
on_backoff=(h[2] for h in hdlrs),
on_giveup=(h[3] for h in hdlrs),
max_tries=3,
jitter=None,
interval=0)
@_save_target
async def emptiness(*args, **kwargs):
pass
await emptiness(1, 2, 3, foo=1, bar=2)
for i in range(3):
assert len(hdlrs[i][0]['success']) == 0
assert len(hdlrs[i][0]['backoff']) == 2
assert len(hdlrs[i][0]['giveup']) == 1
details = dict(hdlrs[i][0]['giveup'][0])
elapsed = details.pop('elapsed')
assert isinstance(elapsed, float)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': emptiness._target,
'tries': 3,
'value': None}
示例11: test_on_predicate
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate(monkeypatch):
monkeypatch.setattr('time.sleep', lambda x: None)
@backoff.on_predicate(backoff.expo)
def return_true(log, n):
val = (len(log) == n - 1)
log.append(val)
return val
log = []
ret = return_true(log, 3)
assert ret is True
assert 3 == len(log)
示例12: test_on_predicate_max_time
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate_max_time(monkeypatch):
nows = [
datetime.datetime(2018, 1, 1, 12, 0, 10, 5),
datetime.datetime(2018, 1, 1, 12, 0, 9, 0),
datetime.datetime(2018, 1, 1, 12, 0, 1, 0),
datetime.datetime(2018, 1, 1, 12, 0, 0, 0),
]
class Datetime:
@staticmethod
def now():
return nows.pop()
monkeypatch.setattr('time.sleep', lambda x: None)
monkeypatch.setattr('datetime.datetime', Datetime)
def giveup(details):
assert details['tries'] == 3
assert details['elapsed'] == 10.000005
@backoff.on_predicate(backoff.expo, jitter=None, max_time=10,
on_giveup=giveup)
def return_true(log, n):
val = (len(log) == n)
log.append(val)
return val
log = []
ret = return_true(log, 10)
assert ret is False
assert len(log) == 3
示例13: test_on_predicate_success
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate_success():
backoffs, giveups, successes = [], [], []
@backoff.on_predicate(backoff.constant,
on_success=successes.append,
on_backoff=backoffs.append,
on_giveup=giveups.append,
jitter=None,
interval=0)
@_save_target
def success(*args, **kwargs):
# succeed after we've backed off twice
return len(backoffs) == 2
success(1, 2, 3, foo=1, bar=2)
# we try 3 times, backing off twice before succeeding
assert len(successes) == 1
assert len(backoffs) == 2
assert len(giveups) == 0
for i in range(2):
details = backoffs[i]
elapsed = details.pop('elapsed')
assert isinstance(elapsed, float)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': success._target,
'tries': i + 1,
'value': False,
'wait': 0}
details = successes[0]
elapsed = details.pop('elapsed')
assert isinstance(elapsed, float)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': success._target,
'tries': 3,
'value': True}
示例14: test_on_predicate_giveup
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate_giveup():
backoffs, giveups, successes = [], [], []
@backoff.on_predicate(backoff.constant,
on_success=successes.append,
on_backoff=backoffs.append,
on_giveup=giveups.append,
max_tries=3,
jitter=None,
interval=0)
@_save_target
def emptiness(*args, **kwargs):
pass
emptiness(1, 2, 3, foo=1, bar=2)
# we try 3 times, backing off twice and giving up once
assert len(successes) == 0
assert len(backoffs) == 2
assert len(giveups) == 1
details = giveups[0]
elapsed = details.pop('elapsed')
assert isinstance(elapsed, float)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': emptiness._target,
'tries': 3,
'value': None}
示例15: test_on_predicate_iterable_handlers
# 需要导入模块: import backoff [as 别名]
# 或者: from backoff import on_predicate [as 别名]
def test_on_predicate_iterable_handlers():
class Logger:
def __init__(self):
self.backoffs = []
self.giveups = []
self.successes = []
loggers = [Logger() for _ in range(3)]
@backoff.on_predicate(backoff.constant,
on_backoff=(l.backoffs.append for l in loggers),
on_giveup=(l.giveups.append for l in loggers),
on_success=(l.successes.append for l in loggers),
max_tries=3,
jitter=None,
interval=0)
@_save_target
def emptiness(*args, **kwargs):
pass
emptiness(1, 2, 3, foo=1, bar=2)
for logger in loggers:
assert len(logger.successes) == 0
assert len(logger.backoffs) == 2
assert len(logger.giveups) == 1
details = dict(logger.giveups[0])
print(details)
elapsed = details.pop('elapsed')
assert isinstance(elapsed, float)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': emptiness._target,
'tries': 3,
'value': None}
# To maintain backward compatibility,
# on_predicate should support 0-argument jitter function.