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: Literal["print"] = "print",
) -> Callable[[Callable[..., R]], Callable[..., R]]
retry(
exceptions: _exceptions = Exception,
tries: int = 1,
delay: int = 0,
print_or_log: Literal["log"] = "log",
) -> Callable[[Callable[..., R]], Callable[..., R]]
retry(
exceptions: _exceptions = Exception,
tries: int = 1,
delay: int = 0,
print_or_log: Literal["print", "log"] = "print",
) -> Callable[[Callable[..., R]], Callable[..., R]]
Summary
Retry a given function a number of times. Catching any known exceptions when they are given. And returning 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
|
Literal['print', 'log']
|
Whether or not the messages should be written to the terminal in a |
'print'
|
Raises:
| Type | Description |
|---|---|
TypeCheckError
|
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 5 | |
# No error
| Example 2: Expected error | |
|---|---|
1 2 3 4 5 | |
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
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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |