Retry Helper Module¶
smonad.retry - helpful utility and decorators for retrying operations.
-
class
smonad.retry.NotReady(value)[source]¶ Bases:
smonad.types.ftry.FailureIndicates target is not ready and retryable
-
class
smonad.retry.StoppedClock[source]¶ This class only exists to make it easier to test retries
-
set_times(times)[source]¶ list of times for the self.time call to return the times can be a single value or be a value + side effect to trigger
- example:
- clock.set_times([100, 200, (300, lambda: Failure(“Uh oh!”), 400])
the 3rd invocation of clock.time() will return the Failure
- example:
- clock.set_times([100, 200, (300, function_with_side_effect), 400])
function_with_side_effect will be triggered the 3rd time clock.time() is invoked
-
-
class
smonad.retry.SystemClock[source]¶ This is just a wrapper around the built-in time.time that makes it much easier to test this module by mocking out time itself.
-
smonad.retry.print_start_message(start_message, start_time)[source]¶ Prints a start message and interpolates the start_time if the {start_time} placeholder is present in the message
- example:
- print_start_message(“Started operation at {start_time}”, start_time)
-
smonad.retry.retry(callable_object, timeout=600, delay=4, start_message=None, silent=False)[source]¶ Retry calling the decorated function until it returns Success, Failure, or the retry count is exceeded Values of Success or Failure are returned immediately A NotReady means to retry the decorated function permitting that there are remaining retries loosely based on: http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/ original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
This decorator expects the wrapped function to return any of three types Failure, NotReady, or Success Note that Failure and Success are imported from smonad.types.ftry and that NotReady is a subclass of Failure
Values of Success or Failure are returned immediately A NotReady means to retry the decorated function permitting that there are remaining retries
Parameters: - callable_object (function) – object that can be called
- timeout (int) – maximum period, in seconds, to wait until an individual try succeeds
- delay (int) – initial delay between retries in seconds
- start_message (str) – message to print before executing first attempt, may contain placeholder {start_time} example start_message=”Started operation at {start_time}”
- silent (bool) – do not print start, end, or progress dots
-
smonad.retry.retry_decorator(timeout=600, delay=4, start_message=None, silent=False)[source]¶ Decorator that wraps a callable with a retry loop. The callable should only return :class:Failure, :class:Success, or :class:NotReady
>>> deadline = time.time() + 300 >>> @retry_decorator() ... def wait_for_deadline(): ... now = time.time() ... if now < deadline: ... return NotReady("not ready yet") ... else: ... return Success("Ready!") >>> wait_for_deadline() Success("Ready!")