xoutil.tasking – Task oriented tools.

Task, multitasking, and concurrent programming tools.

Warning

Experimental. API is not settled.

New in version 1.8.0.

class xoutil.tasking.retrier(max_tries=None, max_time=None, wait=DEFAULT_WAIT_INTERVAL, retry_only=None)[source]

An auto-retrying dispatcher.

A retrier it’s a callable that takes another callable (func) and its arguments and runs it in an auto-retrying loop.

If func raises any exception that is in retry_only, and it has being tried less than max_tries and the time spent executing the function (waiting included) has not reached max_time, the function will be retried.

wait can be a callable or a number. If wait is callable, it must take a single argument with the previous waiting we did (None for the first retry) and return the number of seconds to wait before retrying.

If wait is a number, we convert it to a callable with ConstantWait(wait).

See also

BackoffWait

If retry_only is None, all exceptions (that inherits from Exception) will be retried. Otherwise, only the exceptions in retry_only will be retried.

Waiting is done with time.sleep(). Time tracking is done with time.monotonic().

New in version 1.8.2.

decorate(fn)[source]

Return fn decorated to run in an auto-retry loop.

You can use this to decorate a function you’ll always run inside a retrying loop:

>>> @retrier(max_tries=5, retry_only=TransientError).decorate
... def read_from_url(url):
...     pass
xoutil.tasking.retry(fn, args=None, kwargs=None, *, max_tries=None, max_time=None, wait=DEFAULT_WAIT_INTERVAL, retry_only=None)[source]

Run fn with args and kwargs in an auto-retrying loop.

See retrier. This is just:

>>> retrier(max_tries=max_tries, max_time=max_time, wait=wait,
...         retry_only=retry_only)(fn, *args, **kwargs)

New in version 1.8.2.

class xoutil.tasking.ConstantWait(wait=DEFAULT_WAIT_INTERVAL)[source]

A constant wait algorithm.

Instances are callables that comply with the need of the wait argument for retrier. This callable always return the same wait value.

We never wait less than MIN_WAIT_INTERVAL.

New in version 1.8.2.

Changed in version 1.9.1: Renamed; it was StandardWait. The old name is kept as a deprecated alias.

class xoutil.tasking.BackoffWait(wait=DEFAULT_WAIT_INTERVAL, backoff=1)[source]

A wait algorithm with an exponential backoff.

Instances are callables that comply with the need of the wait argument for retrier.

At each call the wait is increased by doubling backoff (given in milliseconds).

We never wait less than MIN_WAIT_INTERVAL.

New in version 1.8.2.

xoutil.tasking.MIN_WAIT_INTERVAL = 0.02

The minimal time (in seconds) to wait between retries.

New in version 1.8.2.

xoutil.tasking.DEFAULT_WAIT_INTERVAL = 0.05

The default time (in seconds) to wait between retries.

New in version 1.8.2.

class xoutil.tasking.StandardWait

A deprecated alias for ConstantAlias.