本文整理汇总了Python中tenacity.stop_after_attempt方法的典型用法代码示例。如果您正苦于以下问题:Python tenacity.stop_after_attempt方法的具体用法?Python tenacity.stop_after_attempt怎么用?Python tenacity.stop_after_attempt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tenacity
的用法示例。
在下文中一共展示了tenacity.stop_after_attempt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_retry_on_conn_error
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [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
)
示例2: test_run_with_advanced_retry
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [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)
示例3: retry_api_call
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [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)
示例4: _fetch
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def _fetch(self, url, verb='get', **kwargs):
"""return response or None in case of failure, try twice"""
@retry(stop=stop_after_attempt(2), wait=wait_fixed(2))
def _inner_fetch(verb='get'):
headers = {
'Authorization': 'apiToken %s' % C.DEFAULT_SHIPPABLE_TOKEN
}
logging.info(u'%s %s' % (verb, url))
http_method = getattr(requests, verb)
resp = http_method(url, headers=headers, **kwargs)
logging.info(u'shippable status code: %s' % resp.status_code)
logging.info(u'shippable reason: %s' % resp.reason)
if resp.status_code not in [200, 302, 400]:
logging.error(u'RC: %s', resp.status_code)
raise TryAgain
return resp
try:
logging.debug(u'%s' % url)
return _inner_fetch(verb=verb)
except RetryError as e:
logging.error(e)
示例5: clone_git_repo
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def clone_git_repo(self, repo_url, repo_dir, rev="origin/branches/default/tip"):
logger.info(f"Cloning {repo_url}...")
if not os.path.exists(repo_dir):
tenacity.retry(
wait=tenacity.wait_exponential(multiplier=1, min=16, max=64),
stop=tenacity.stop_after_attempt(5),
)(
lambda: subprocess.run(
["git", "clone", "--quiet", repo_url, repo_dir], check=True
)
)()
tenacity.retry(
wait=tenacity.wait_exponential(multiplier=1, min=16, max=64),
stop=tenacity.stop_after_attempt(5),
)(
lambda: subprocess.run(
["git", "fetch"], cwd=repo_dir, capture_output=True, check=True,
)
)()
subprocess.run(
["git", "checkout", rev], cwd=repo_dir, capture_output=True, check=True
)
示例6: test_retry_child_class_with_override_backward_compat
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_retry_child_class_with_override_backward_compat(self):
class MyStop(tenacity.stop_after_attempt):
def __init__(self):
super(MyStop, self).__init__(1)
def __call__(self, attempt_number, seconds_since_start):
return super(MyStop, self).__call__(
attempt_number, seconds_since_start)
retrying = Retrying(wait=tenacity.wait_fixed(0.01),
stop=MyStop())
def failing():
raise NotImplementedError()
with pytest.raises(RetryError):
retrying.call(failing)
示例7: test_wait_backward_compat_with_result
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_wait_backward_compat_with_result(self):
captures = []
def wait_capture(attempt, delay, last_result=None):
captures.append(last_result)
return 1
def dying():
raise Exception("Broken")
r_attempts = 10
r = Retrying(wait=wait_capture, sleep=lambda secs: None,
stop=tenacity.stop_after_attempt(r_attempts),
reraise=True)
with reports_deprecation_warning():
self.assertRaises(Exception, r.call, dying)
self.assertEqual(r_attempts - 1, len(captures))
self.assertTrue(all([r.failed for r in captures]))
示例8: test_before_attempts
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_before_attempts(self):
TestBeforeAfterAttempts._attempt_number = 0
def _before(retry_state):
TestBeforeAfterAttempts._attempt_number = \
retry_state.attempt_number
@retry(wait=tenacity.wait_fixed(1),
stop=tenacity.stop_after_attempt(1),
before=_before)
def _test_before():
pass
_test_before()
self.assertTrue(TestBeforeAfterAttempts._attempt_number == 1)
示例9: test_after_attempts
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_after_attempts(self):
TestBeforeAfterAttempts._attempt_number = 0
def _after(retry_state):
TestBeforeAfterAttempts._attempt_number = \
retry_state.attempt_number
@retry(wait=tenacity.wait_fixed(0.1),
stop=tenacity.stop_after_attempt(3),
after=_after)
def _test_after():
if TestBeforeAfterAttempts._attempt_number < 2:
raise Exception("testing after_attempts handler")
else:
pass
_test_after()
self.assertTrue(TestBeforeAfterAttempts._attempt_number == 2)
示例10: test_before_sleep_backward_compat
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_before_sleep_backward_compat(self):
def _before_sleep(retry_obj, sleep, last_result):
self.assertGreater(sleep, 0)
_before_sleep.attempt_number = \
retry_obj.statistics['attempt_number']
_before_sleep.attempt_number = 0
@retry(wait=tenacity.wait_fixed(0.01),
stop=tenacity.stop_after_attempt(3),
before_sleep=_before_sleep)
def _test_before_sleep():
if _before_sleep.attempt_number < 2:
raise Exception("testing before_sleep_attempts handler")
with reports_deprecation_warning():
_test_before_sleep()
self.assertEqual(_before_sleep.attempt_number, 2)
示例11: test_before_sleep_log_returns
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_before_sleep_log_returns(self, exc_info=False):
thing = NoneReturnUntilAfterCount(2)
logger = logging.getLogger(self.id())
logger.propagate = False
logger.setLevel(logging.INFO)
handler = CapturingHandler()
logger.addHandler(handler)
try:
_before_sleep = tenacity.before_sleep_log(logger,
logging.INFO,
exc_info=exc_info)
_retry = tenacity.retry_if_result(lambda result: result is None)
retrying = Retrying(wait=tenacity.wait_fixed(0.01),
stop=tenacity.stop_after_attempt(3),
retry=_retry, before_sleep=_before_sleep)
retrying.call(thing.go)
finally:
logger.removeHandler(handler)
etalon_re = r'^Retrying .* in 0\.01 seconds as it returned None\.$'
self.assertEqual(len(handler.records), 2)
fmt = logging.Formatter().format
self.assertRegexpMatches(fmt(handler.records[0]), etalon_re)
self.assertRegexpMatches(fmt(handler.records[1]), etalon_re)
示例12: test_reraise_from_retry_error
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_reraise_from_retry_error(self):
calls = []
@retry(wait=tenacity.wait_fixed(0.1),
stop=tenacity.stop_after_attempt(2))
def _raise_key_error():
calls.append('x')
raise KeyError("Bad key")
def _reraised_key_error():
try:
_raise_key_error()
except tenacity.RetryError as retry_err:
retry_err.reraise()
self.assertRaises(KeyError, _reraised_key_error)
self.assertEqual(2, len(calls))
示例13: test_reraise_timeout_from_retry_error
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_reraise_timeout_from_retry_error(self):
calls = []
@retry(wait=tenacity.wait_fixed(0.1),
stop=tenacity.stop_after_attempt(2),
retry=lambda retry_state: True)
def _mock_fn():
calls.append('x')
def _reraised_mock_fn():
try:
_mock_fn()
except tenacity.RetryError as retry_err:
retry_err.reraise()
self.assertRaises(tenacity.RetryError, _reraised_mock_fn)
self.assertEqual(2, len(calls))
示例14: test_retry_error_callback_backward_compat
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_retry_error_callback_backward_compat(self):
num_attempts = 3
def retry_error_callback(fut):
retry_error_callback.called_times += 1
return fut
retry_error_callback.called_times = 0
@retry(stop=tenacity.stop_after_attempt(num_attempts),
retry_error_callback=retry_error_callback)
def _foobar():
self._attempt_number += 1
raise Exception("This exception should not be raised")
with reports_deprecation_warning():
result = _foobar()
self.assertEqual(retry_error_callback.called_times, 1)
self.assertEqual(num_attempts, self._attempt_number)
self.assertIsInstance(result, tenacity.Future)
示例15: test_retry_error_callback
# 需要导入模块: import tenacity [as 别名]
# 或者: from tenacity import stop_after_attempt [as 别名]
def test_retry_error_callback(self):
num_attempts = 3
def retry_error_callback(retry_state):
retry_error_callback.called_times += 1
return retry_state.outcome
retry_error_callback.called_times = 0
@retry(stop=tenacity.stop_after_attempt(num_attempts),
retry_error_callback=retry_error_callback)
def _foobar():
self._attempt_number += 1
raise Exception("This exception should not be raised")
result = _foobar()
self.assertEqual(retry_error_callback.called_times, 1)
self.assertEqual(num_attempts, self._attempt_number)
self.assertIsInstance(result, tenacity.Future)