pyiron_snippets.retry module

pyiron_snippets.retry.retry(func: Callable[[], T], error: type[Exception] | tuple[type[Exception], ...], msg: str, at_most: int | None = None, delay: float = 1.0, delay_factor: float = 1.0, log: bool | Any = True) T[source]

Try to call func until it no longer raises error.

Any other exception besides error is still raised.

Parameters:
  • func (callable) – function to call, should take no arguments

  • error (Exception or tuple thereof) – any exceptions to be caught

  • msg (str) – messing to be written to the log if error occurs.

  • at_most (int, optional) – retry at most this many times, None means retry forever

  • delay (float) – time to wait between retries in seconds

  • delay_factor (float) – multiply delay between retries by this factor

  • logger (bool|object) – Whether to pass a message to warnings.warn on each retry. (Default is True.) Optionally, an object with a warn() method can be passed and the message will be sent there instead (e.g. pyiron_snippets.logger.logger).

Raises:
  • error – if at_most is exceeded the last error is re-raised

  • Exception – any exception raised by func that does not match error

Returns:

whatever is returned by func

Return type:

object