Python tenacity.Retrying() Examples
The following are 30
code examples of tenacity.Retrying().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tenacity
, or try the search function
.
Example #1
Source File: util.py From PyAthena with MIT License | 7 votes |
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)
Example #2
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 6 votes |
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]))
Example #4
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 6 votes |
def test_wait_chain_multiple_invocations(self): sleep_intervals = [] r = Retrying( sleep=sleep_intervals.append, wait=tenacity.wait_chain(*[ tenacity.wait_fixed(i + 1) for i in six.moves.range(3) ]), stop=tenacity.stop_after_attempt(5), retry=tenacity.retry_if_result(lambda x: x == 1), ) @r.wraps def always_return_1(): return 1 self.assertRaises(tenacity.RetryError, always_return_1) self.assertEqual(sleep_intervals, [1.0, 2.0, 3.0, 3.0]) sleep_intervals[:] = [] # Clear and restart retrying. self.assertRaises(tenacity.RetryError, always_return_1) self.assertEqual(sleep_intervals, [1.0, 2.0, 3.0, 3.0]) sleep_intervals[:] = []
Example #5
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 6 votes |
def test_before_sleep_log_raises(self): thing = NoIOErrorAfterCount(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) retrying = Retrying(wait=tenacity.wait_fixed(0.01), stop=tenacity.stop_after_attempt(3), before_sleep=_before_sleep) retrying.call(thing.go) finally: logger.removeHandler(handler) etalon_re = (r"^Retrying .* in 0\.01 seconds as it raised " r"(IO|OS)Error: Hi there, I'm an IOError\.$") 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)
Example #6
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 6 votes |
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)
Example #7
Source File: driver.py From zun with Apache License 2.0 | 6 votes |
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)
Example #8
Source File: driver.py From zun with Apache License 2.0 | 6 votes |
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)
Example #9
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_retry_try_again_forever(self): def _r(): raise tenacity.TryAgain r = Retrying(stop=tenacity.stop_after_attempt(5), retry=tenacity.retry_never) self.assertRaises(tenacity.RetryError, r.call, _r) self.assertEqual(5, r.statistics['attempt_number'])
Example #10
Source File: test_functions.py From qinling with Apache License 2.0 | 5 votes |
def test_detach(self): """Admin only operation.""" function_id = self.create_function(self.python_zip_file) resp, _ = self.client.create_execution( function_id, input='{"name": "Qinling"}' ) self.assertEqual(201, resp.status) resp, body = self.admin_client.get_function_workers(function_id) self.assertEqual(200, resp.status) self.assertEqual(1, len(body['workers'])) # Detach function resp, _ = self.admin_client.detach_function(function_id) self.assertEqual(202, resp.status) def _assert_workers(): resp, body = self.admin_client.get_function_workers(function_id) self.assertEqual(200, resp.status) self.assertEqual(0, len(body['workers'])) r = tenacity.Retrying( wait=tenacity.wait_fixed(1), stop=tenacity.stop_after_attempt(5), retry=tenacity.retry_if_exception_type(AssertionError) ) r.call(_assert_workers)
Example #11
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_wait_func(self): def wait_func(retry_state): return retry_state.attempt_number * retry_state.seconds_since_start r = Retrying(wait=wait_func) self.assertEqual(r.wait(make_retry_state(1, 5)), 5) self.assertEqual(r.wait(make_retry_state(2, 11)), 22) self.assertEqual(r.wait(make_retry_state(10, 100)), 1000)
Example #12
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
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)
Example #13
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
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)
Example #14
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_wait_arbitrary_sum(self): r = Retrying(wait=sum([tenacity.wait_fixed(1), tenacity.wait_random(0, 3), tenacity.wait_fixed(5), tenacity.wait_none()])) # 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)
Example #15
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_wait_chain(self): r = Retrying(wait=tenacity.wait_chain( *[tenacity.wait_fixed(1) for i in six.moves.range(2)] + [tenacity.wait_fixed(4) for i in six.moves.range(2)] + [tenacity.wait_fixed(8) for i in six.moves.range(1)])) for i in six.moves.range(10): w = r.wait(i + 1, 1) if i < 2: self._assert_range(w, 1, 2) elif i < 4: self._assert_range(w, 4, 5) else: self._assert_range(w, 8, 9)
Example #16
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_wait_backward_compat(self): """Ensure Retrying object accepts both old and newstyle wait funcs.""" def wait1(previous_attempt_number, delay_since_first_attempt): wait1.calls.append(( previous_attempt_number, delay_since_first_attempt)) return 0 wait1.calls = [] def wait2(previous_attempt_number, delay_since_first_attempt, last_result): wait2.calls.append(( previous_attempt_number, delay_since_first_attempt, last_result)) return 0 wait2.calls = [] def dying(): raise Exception("Broken") retrying1 = Retrying(wait=wait1, stop=tenacity.stop_after_attempt(4)) with reports_deprecation_warning(): self.assertRaises(Exception, lambda: retrying1.call(dying)) self.assertEqual([t[0] for t in wait1.calls], [1, 2, 3]) # This assumes that 3 iterations complete within 1 second. self.assertTrue(all(t[1] < 1 for t in wait1.calls)) retrying2 = Retrying(wait=wait2, stop=tenacity.stop_after_attempt(4)) with reports_deprecation_warning(): self.assertRaises(Exception, lambda: retrying2.call(dying)) self.assertEqual([t[0] for t in wait2.calls], [1, 2, 3]) # This assumes that 3 iterations complete within 1 second. self.assertTrue(all(t[1] < 1 for t in wait2.calls)) self.assertEqual([str(t[2].exception()) for t in wait2.calls], ['Broken'] * 3)
Example #17
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_context_manager_reraise(self): from tenacity import Retrying class CustomError(Exception): pass retry = Retrying(reraise=True, stop=tenacity.stop_after_attempt(2)) def test(): for attempt in retry: with attempt: raise CustomError("Don't retry!") self.assertRaises(CustomError, test)
Example #18
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_retry_try_again_forever_reraise(self): def _r(): raise tenacity.TryAgain r = Retrying(stop=tenacity.stop_after_attempt(5), retry=tenacity.retry_never, reraise=True) self.assertRaises(tenacity.TryAgain, r, _r) self.assertEqual(5, r.statistics['attempt_number'])
Example #19
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_retry_function_object(self): """Test that six.wraps doesn't cause problems with callable objects. It raises an error upon trying to wrap it in Py2, because __name__ attribute is missing. It's fixed in Py3 but was never backported. """ class Hello(object): def __call__(self): return "Hello" retrying = Retrying(wait=tenacity.wait_fixed(0.01), stop=tenacity.stop_after_attempt(3)) h = retrying.wraps(Hello()) self.assertEqual(h(), "Hello")
Example #20
Source File: s3.py From gnocchi with Apache License 2.0 | 5 votes |
def _put_object_safe(self, Bucket, Key, Body): put = self.s3.put_object(Bucket=Bucket, Key=Key, Body=Body) if self._consistency_stop: def _head(): return self.s3.head_object(Bucket=Bucket, Key=Key, IfMatch=put['ETag']) tenacity.Retrying( retry=tenacity.retry_if_result( lambda r: r['ETag'] != put['ETag']), wait=self._consistency_wait, stop=self._consistency_stop)(_head)
Example #21
Source File: test_function_versions.py From qinling with Apache License 2.0 | 5 votes |
def test_detach(self): """Admin only operation.""" function_id = self.create_function() version = self.create_function_version(function_id) # Create execution to allocate worker resp, _ = self.client.create_execution( function_id, input='{"name": "Qinling"}', version=version ) self.assertEqual(201, resp.status) resp, body = self.admin_client.get_function_workers(function_id, version=version) self.assertEqual(200, resp.status) self.assertEqual(1, len(body['workers'])) # Detach function version from workers resp, _ = self.admin_client.detach_function(function_id, version=version) self.assertEqual(202, resp.status) def _assert_workers(): resp, body = self.admin_client.get_function_workers( function_id, version=version ) self.assertEqual(200, resp.status) self.assertEqual(0, len(body['workers'])) r = tenacity.Retrying( wait=tenacity.wait_fixed(1), stop=tenacity.stop_after_attempt(5), retry=tenacity.retry_if_exception_type(AssertionError) ) r.call(_assert_workers)
Example #22
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_context_manager_retry_one(self): from tenacity import Retrying raise_ = True for attempt in Retrying(): with attempt: if raise_: raise_ = False raise Exception("Retry it!")
Example #23
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_context_manager_on_error(self): from tenacity import Retrying class CustomError(Exception): pass retry = Retrying(retry=tenacity.retry_if_exception_type(IOError)) def test(): for attempt in retry: with attempt: raise CustomError("Don't retry!") self.assertRaises(CustomError, test)
Example #24
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_context_manager_retry_error(self): from tenacity import Retrying retry = Retrying(stop=tenacity.stop_after_attempt(2)) def test(): for attempt in retry: with attempt: raise Exception("Retry it!") self.assertRaises(RetryError, test)
Example #25
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_exponential_with_min_wait_and_multiplier(self): r = Retrying(wait=tenacity.wait_exponential( min=20, multiplier=2)) self.assertEqual(r.wait(1, 0), 20) self.assertEqual(r.wait(2, 0), 20) self.assertEqual(r.wait(3, 0), 20) self.assertEqual(r.wait(4, 0), 20) self.assertEqual(r.wait(5, 0), 32) self.assertEqual(r.wait(6, 0), 64) self.assertEqual(r.wait(7, 0), 128) self.assertEqual(r.wait(8, 0), 256) self.assertEqual(r.wait(20, 0), 1048576)
Example #26
Source File: utils.py From gnocchi with Apache License 2.0 | 5 votes |
def retry_on_exception_and_log(msg): return tenacity.Retrying( wait=wait_exponential, retry=_retry_on_exception_and_log(msg)).wraps
Example #27
Source File: http.py From airflow with Apache License 2.0 | 5 votes |
def run_with_advanced_retry(self, _retry_args, *args, **kwargs): """ Runs Hook.run() with a Tenacity decorator attached to it. This is useful for connectors which might be disturbed by intermittent issues and should not instantly fail. :param _retry_args: Arguments which define the retry behaviour. See Tenacity documentation at https://github.com/jd/tenacity :type _retry_args: dict .. code-block:: python hook = HttpHook(http_conn_id='my_conn',method='GET') retry_args = dict( wait=tenacity.wait_exponential(), stop=tenacity.stop_after_attempt(10), retry=requests.exceptions.ConnectionError ) hook.run_with_advanced_retry( endpoint='v1/test', _retry_args=retry_args ) """ self._retry_obj = tenacity.Retrying( **_retry_args ) return self._retry_obj(self.run, *args, **kwargs)
Example #28
Source File: request_factory.py From jsonapi-requests with BSD 3-Clause "New" or "Revised" License | 5 votes |
def retrying(self): retry_condition = ( tenacity.retry_if_exception_type(ApiConnectionError) | tenacity.retry_if_exception_type(ApiInternalServerError) ) return tenacity.Retrying( reraise=True, retry=retry_condition, stop=tenacity.stop_after_attempt(self.config.RETRIES) )
Example #29
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_never_stop(self): r = Retrying() self.assertFalse(r.stop(make_retry_state(3, 6546)))
Example #30
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_stop_after_attempt(self): r = Retrying(stop=tenacity.stop_after_attempt(3)) self.assertFalse(r.stop(make_retry_state(2, 6546))) self.assertTrue(r.stop(make_retry_state(3, 6546))) self.assertTrue(r.stop(make_retry_state(4, 6546)))