Retry
toolbox_python.retry
🔗
Summary
The retry
module is for enabling automatic retrying of a given function when a specific Exception
is thrown.
retry
🔗
retry(
exceptions: _exceptions = Exception,
tries: int = 1,
delay: int = 0,
print_or_log: Optional[
Literal["print", "log"]
] = "print",
) -> Optional[Any]
Summary
Retry a given function a number of times. Catching any known exceptions when they are given. And retrurning any output to either a terminal or a log file.
Deprecated
This function is deprecated. Please use the retry()
decorator from the stamina
package instead.
For more info, see: Docs, GitHub, PyPi.
Details
This function should always be implemented as a decorator.
It is written based on the premise that a certain process may fail and return a given message, but that is known and expected, and you just want to wait a second or so then retry again.
Typically, this is seen in async processes, or when writing data to a delta
table when there may be concurrent read/writes occurring at the same time. In these instances, you will know the error message and can re-try again a certain number of times.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exceptions
|
_exceptions
|
A given single or collection of expected exceptions for which to catch and retry for. |
Exception
|
tries
|
int
|
The number of retries to attempt. If the underlying process is still failing after this number of attempts, then throw a hard error and alert the user. |
1
|
delay
|
int
|
The number of seconds to delay between each retry. |
0
|
print_or_log
|
Optional[Literal['print', 'log']]
|
Whether or not the messages should be written to the terminal in a |
'print'
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ValueError
|
If either |
RuntimeError
|
If either an unexpected |
Returns:
Name | Type | Description |
---|---|---|
result |
Optional[Any]
|
The result from the underlying function, if any. |
Examples
Imports | |
---|---|
1 |
|
Example 1: No error | |
---|---|
1 2 3 4 |
|
# No error
Example 2: Expected error | |
---|---|
1 2 3 4 |
|
Caught an expected error at iteration 1: `ValueError`. Retrying in 1 seconds...
Caught an expected error at iteration 2: `ValueError`. Retrying in 1 seconds...
Caught an expected error at iteration 3: `ValueError`. Retrying in 1 seconds...
Caught an expected error at iteration 4: `ValueError`. Retrying in 1 seconds...
Caught an expected error at iteration 5: `ValueError`. Retrying in 1 seconds...
RuntimeError: Still could not write after 5 iterations. Please check.
Credit
Inspiration from:
Source code in src/toolbox_python/retry.py
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|