File: README.md

package info (click to toggle)
mozjs78 78.15.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 739,892 kB
  • sloc: javascript: 1,344,214; cpp: 1,215,708; python: 526,544; ansic: 433,835; xml: 118,736; sh: 26,176; asm: 16,664; makefile: 11,537; yacc: 4,486; perl: 2,564; ada: 1,681; lex: 1,414; pascal: 1,139; cs: 879; exp: 499; java: 164; ruby: 68; sql: 45; csh: 35; sed: 18; lisp: 2
file content (147 lines) | stat: -rw-r--r-- 5,224 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

# Redo - Utilities to retry Python callables
******************************************

## Introduction

Redo provides various means to add seamless ability to retry to any Python callable. Redo includes a plain function `(redo.retry)`, a decorator `(redo.retriable)`, and a context manager `(redo.retrying)` to enable you to integrate it in the best possible way for your project. As a bonus, a standalone interface is also included `("retry")`.

## Installation

For installing with pip, run following commands
> pip install redo

## How To Use
Below is the list of functions available
* retrier
* retry
* retriable
* retrying (contextmanager)

### retrier(attempts=5, sleeptime=10, max_sleeptime=300, sleepscale=1.5, jitter=1)
A generator function that sleeps between retries, handles exponential back off and jitter. The action you are retrying is meant to run after retrier yields. At each iteration, we sleep for `sleeptime + random.randint(-jitter, jitter)`. Afterwards sleeptime is multiplied by sleepscale for the next iteration.

**Arguments Detail:**    

1. **attempts (int):** maximum number of times to try; defaults to 5
2. **sleeptime (float):** how many seconds to sleep between tries; defaults to 60s (one minute)
3. **max_sleeptime (float):** the longest we'll sleep, in seconds; defaults to 300s (five minutes)
4. **sleepscale (float):** how much to multiply the sleep time by each iteration; defaults to 1.5
5. **jitter (int):** random jitter to introduce to sleep time each iteration. the amount is chosen at random between `[-jitter, +jitter]` defaults to 1

**Output:**
None, a maximum of `attempts` number of times

**Example:**

    >>> n = 0
    >>> for _ in retrier(sleeptime=0, jitter=0):
    ...     if n == 3:
    ...         # We did the thing!
    ...         break
    ...     n += 1
    >>> n
    3
    >>> n = 0
    >>> for _ in retrier(sleeptime=0, jitter=0):
    ...     if n == 6:
    ...         # We did the thing!
    ...         break
    ...     n += 1
    ... else:
    ...     print("max tries hit")
    max tries hit

### retry(action, attempts=5, sleeptime=60, max_sleeptime=5 * 60, sleepscale=1.5, jitter=1, retry_exceptions=(Exception,), cleanup=None, args=(), kwargs={})  
Calls an action function until it succeeds, or we give up.

**Arguments Detail:**  

1. **action (callable):** the function to retry
2. **attempts (int):** maximum number of times to try; defaults to 5
3. **sleeptime (float):** how many seconds to sleep between tries; defaults to 60s (one minute)
4. **max_sleeptime (float):** the longest we'll sleep, in seconds; defaults to 300s (five minutes)
5. **sleepscale (float):** how much to multiply the sleep time by each iteration; defaults to 1.5
6. **jitter (int):** random jitter to introduce to sleep time each iteration. The amount is chosen at random between `[-jitter, +jitter]` defaults to 1
7. **retry_exceptions (tuple):** tuple of exceptions to be caught. If other exceptions are raised by `action()`, then these are immediately re-raised to the caller.
8. **cleanup (callable):** optional; called if one of `retry_exceptions` is caught. No arguments are passed to the cleanup function; if your cleanup requires arguments, consider using `functools.partial` or a `lambda` function.
9. **args (tuple):** positional arguments to call `action` with
10. **kwargs (dict):** keyword arguments to call `action` with

**Output:**
 Whatever action`(*args, **kwargs)` returns
 
 **Output:**
        Whatever action(*args, **kwargs) raises. `retry_exceptions` are caught
        up until the last attempt, in which case they are re-raised.

**Example:**

    >>> count = 0
    >>> def foo():
    ...     global count
    ...     count += 1
    ...     print(count)
    ...     if count < 3:
    ...         raise ValueError("count is too small!")
    ...     return "success!"
    >>> retry(foo, sleeptime=0, jitter=0)
    1
    2
    3
    'success!'

### retriable(*retry_args, **retry_kwargs)
A decorator factory for `retry()`. Wrap your function in `@retriable(...)` to give it retry powers!

**Arguments Detail:**  
        Same as for `retry`, with the exception of `action`, `args`, and `kwargs`,
        which are left to the normal function definition.

**Output:**
A function decorator

**Example:**

    >>> count = 0
    >>> @retriable(sleeptime=0, jitter=0)
    ... def foo():
    ...     global count
    ...     count += 1
    ...     print(count)
    ...     if count < 3:
    ...         raise ValueError("count too small")
    ...     return "success!"
    >>> foo()
    1
    2
    3
    'success!'

### retrying(func, *retry_args, **retry_kwargs)
A context manager for wrapping functions with retry functionality.

**Arguments Detail:**   

1. **func (callable):** the function to wrap
other arguments as per `retry`

**Output:**
A context manager that returns `retriable(func)` on `__enter__`

**Example:**

    >>> count = 0
    >>> def foo():
    ...     global count
    ...     count += 1
    ...     print(count)
    ...     if count < 3:
    ...         raise ValueError("count too small")
    ...     return "success!"
    >>> with retrying(foo, sleeptime=0, jitter=0) as f:
    ...     f()
    1
    2
    3
    'success!'