From f25117ae8d31d70e5940f553704773d7b64d272f Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Thu, 16 Jul 2015 15:18:34 -0700 Subject: [PATCH 1/2] Allow providing alternate sleep functions This can be quite useful in say eventlet usage (which provides a alternate eventlet.sleep function that correctly integrates with eventlet); or it could allow others to provide callbacks that log the sleep that is about to happen or ... so this adds a __init__ argument to provide a alternate sleeping function. --- retrying.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/retrying.py b/retrying.py index 3ed312d..8c0b7d0 100644 --- a/retrying.py +++ b/retrying.py @@ -68,7 +68,8 @@ def __init__(self, wrap_exception=False, stop_func=None, wait_func=None, - wait_jitter_max=None): + wait_jitter_max=None, + sleep_func=time.sleep): self._stop_max_attempt_number = 5 if stop_max_attempt_number is None else stop_max_attempt_number self._stop_max_delay = 100 if stop_max_delay is None else stop_max_delay @@ -137,6 +138,7 @@ def __init__(self, self._retry_on_result = retry_on_result self._wrap_exception = wrap_exception + self._sleep_func = sleep_func def stop_after_attempt(self, previous_attempt_number, delay_since_first_attempt_ms): """Stop after the previous attempt >= stop_max_attempt_number.""" @@ -217,7 +219,7 @@ def call(self, fn, *args, **kwargs): if self._wait_jitter_max: jitter = random.random() * self._wait_jitter_max sleep = sleep + max(0, jitter) - time.sleep(sleep / 1000.0) + self.sleep_func(sleep / 1000.0) attempt_number += 1 From 9ad6030ce6dbd0505faf06aff3444d427cfc0a69 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Sat, 18 Jul 2015 20:58:24 -0700 Subject: [PATCH 2/2] Fix to use the right variable name --- retrying.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retrying.py b/retrying.py index 8c0b7d0..a683661 100644 --- a/retrying.py +++ b/retrying.py @@ -219,7 +219,7 @@ def call(self, fn, *args, **kwargs): if self._wait_jitter_max: jitter = random.random() * self._wait_jitter_max sleep = sleep + max(0, jitter) - self.sleep_func(sleep / 1000.0) + self._sleep_func(sleep / 1000.0) attempt_number += 1