Exception trap
Project description
exctrap: Trap exceptions for retry
It is not uncommon to have Python code that requires exceptions to be trapped, with the code triggering it retried, e.g., to handle temporary network or host failures when fetching web resources. There are quite a few Python packages that provide this functionality. Or actually, two functionalities: (1) trapping the exception, and (2) retrying the triggering code upon failure.
Typically, they work in the level of function: the code that needs to be retried is written as a function, and it is either wrapped within a context manager or passed to a retry function so that the function is invoked repeatedly until it succeeds or fail sufficiently many times. In this way, the two functionalities are packed into one function or context manager for the user.
We take a slightly different approach: the two functionalities are separated into two entities: a context manager that traps exception, and a function that returns the context manager. We argue that this leads to neater code.
Recipe
This provides most of what we normally need.
for etrapper in exctrap.trial():
with etrapper:
# Do whatever that may fail.
# Will reach here whether or not an exception is raised.
# If you want to reraise the exception here, use etrapper.reraise().
# If retry fails, the last exception is reraised so this is not reached.
Options
The trial()
function provides all the options. These include:
num_tries
: Maximum number of trials, if the code keeps raising exceptions for this many tries the exception is reraised without further retry.retry_period
: Number of seconds to wait between tries (adjusted byperiod_noise
).period_noise
: Add or subtract at most this fraction of theretry_period
to get the actual amount of seconds to sleep.etypes
: Exception types to trap. By default, trap all exceptions derived fromException
. This is passed to the constructor ofExcTrapper
when creating exception trappers.
Note that this does not require any function to be created for the code needs to be exception-proof. Experience shows that code can be a lot more neat as a result: the code can access the required variables much more easily when it does not sit within a separate global function or an inner function.
Implementation
The "etrapper" is the context manager which traps exceptions when
running under the "with" statement. This is done simply by recording
exceptions in the trapper object, and returning True to swallow it, in
the __exit__
function. If needed, the exception can be asked to be
reraised()
.
The trial()
function provides the retry logic and create exception
trappers. Because the exception would be trapped in the trapper, all
trial needs to do is to check whether an exception is trapped, and
decide whether to retry or reraise.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file exctrap-0.2.tar.gz
.
File metadata
- Download URL: exctrap-0.2.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d93675ffdddef9650ed18f254f3b38252382ecbae84970c0a2a21e306a718d7e |
|
MD5 | c9debf9e7ff2dca1996f6a6aa5502965 |
|
BLAKE2b-256 | b5a5caac6429b169721cbc0365a98b39ed9a452d05855a2901b930fde85d7374 |
File details
Details for the file exctrap-0.2-py3-none-any.whl
.
File metadata
- Download URL: exctrap-0.2-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35f6d1c07bfc5b4f4a8ee8b24529e1558f324717b4fbca945056d0f79721f722 |
|
MD5 | d77980701ff56d1d4026f02914b7696d |
|
BLAKE2b-256 | 4ff5d6df8212d3745ce0962521f505df2956980066b38c912151b0113bfbb2a6 |