Tiny Retry
What is TinyRetry?
tinyretry is a python module that provides failure retry encapsulation for the target function
Programming Language
- python 2.7
- python 3.7
Install
pip install tinyretry
Quick Start
use python decorators syntax to wrap the call target
Case 1: retry until the return code equals to the special value
When we call the function and hope to get the special return code, then we can use RetryUntilForJson or RetryUntilForBool
from tinyretry import RetryUntilForJson
@RetryUntilForJson(retry_code_key='errno', expected_err_code=0, max_retry_num = 10, retry_interval_sec = 0.1)
def DoHttpRequest1():
errno = random.randint(0,2)
errmsg_arr = ['ok', 'err-timeout', 'err-req-limit']
return {
"errno": errno,
"errmsg": errmsg_arr[errno]
}
class Test_TinyRetry(unittest.TestCase):
def test_RetryUntilForJson(self):
for i in range(5):
res = DoHttpRequest1()
self.assertEqual(res["errno"], 0)
Then you can get the return value from the function DoHttpRequest1() which must be successful, the call log like this:
bogon:tinyretry jamesqin$ python tinyretry_test.py
2020-02-14 14:03:14,754 - root - INFO - call RetryUntilForJson(DoHttpRequest)(...)
2020-02-14 14:03:14,754 - root - INFO - Get expected error code 0
2020-02-14 14:03:14,754 - root - INFO - call RetryUntilForJson(DoHttpRequest)(...)
2020-02-14 14:03:14,754 - root - INFO - Error code 2 was found, retry 1
2020-02-14 14:03:14,859 - root - INFO - Get expected error code 0
2020-02-14 14:03:14,860 - root - INFO - call RetryUntilForJson(DoHttpRequest)(...)
2020-02-14 14:03:14,860 - root - INFO - Error code 1 was found, retry 1
2020-02-14 14:03:14,964 - root - INFO - Error code 1 was found, retry 2
2020-02-14 14:03:15,070 - root - INFO - Error code 1 was found, retry 3
2020-02-14 14:03:15,172 - root - INFO - Error code 2 was found, retry 4
2020-02-14 14:03:15,278 - root - INFO - Get expected error code 0
2020-02-14 14:03:15,278 - root - INFO - call RetryUntilForJson(DoHttpRequest)(...)
2020-02-14 14:03:15,278 - root - INFO - Error code 2 was found, retry 1
2020-02-14 14:03:15,381 - root - INFO - Error code 1 was found, retry 2
2020-02-14 14:03:15,482 - root - INFO - Error code 1 was found, retry 3
2020-02-14 14:03:15,586 - root - INFO - Error code 1 was found, retry 4
2020-02-14 14:03:15,692 - root - INFO - Error code 2 was found, retry 5
2020-02-14 14:03:15,794 - root - INFO - Get expected error code 0
2020-02-14 14:03:15,794 - root - INFO - call RetryUntilForJson(DoHttpRequest)(...)
2020-02-14 14:03:15,794 - root - INFO - Get expected error code 0
.
----------------------------------------------------------------------
Ran 1 test in 1.040s
OK
Case 2: while the return code equals to the special value, we will retry
When we call the function and hope to skip the special return code, then we can use RetryWhileForJson or RetryWhileForBool
from tinyretry import RetryWhileForJson
@RetryWhileForJson(retry_code_key='errno', not_expected_err_code=2, max_retry_num = 10, retry_interval_sec = 0.1)
def DoHttpRequest2():
errno = random.randint(0,2)
errmsg_arr = ['ok', 'err-timeout', 'err-req-limit']
return {
"errno": errno,
"errmsg": errmsg_arr[errno]
}
class Test_TinyRetry(unittest.TestCase):
def test_RetryWhileForJson(self):
for i in range(5):
res = DoHttpRequest2()
self.assertNotEqual(res["errno"], 2)
then call log look like this:
2020-02-14 14:13:30,708 - root - INFO - call RetryWhileForJson(DoHttpRequest2)(...)
2020-02-14 14:13:30,708 - root - INFO - Error code 2 was found, retry 1
2020-02-14 14:13:30,810 - root - INFO - Error code 2 was found, retry 2
2020-02-14 14:13:30,912 - root - INFO - Error code 2 was found, retry 3
2020-02-14 14:13:31,013 - root - INFO - Error code 2 was found, retry 4
2020-02-14 14:13:31,117 - root - INFO - Error code 2 was found, retry 5
2020-02-14 14:13:31,222 - root - INFO - Get not expected error code 1
2020-02-14 14:13:31,222 - root - INFO - call RetryWhileForJson(DoHttpRequest2)(...)
2020-02-14 14:13:31,222 - root - INFO - Get not expected error code 0
2020-02-14 14:13:31,222 - root - INFO - call RetryWhileForJson(DoHttpRequest2)(...)
2020-02-14 14:13:31,222 - root - INFO - Error code 2 was found, retry 1
2020-02-14 14:13:31,324 - root - INFO - Get not expected error code 0
2020-02-14 14:13:31,324 - root - INFO - call RetryWhileForJson(DoHttpRequest2)(...)
2020-02-14 14:13:31,324 - root - INFO - Error code 2 was found, retry 1
2020-02-14 14:13:31,426 - root - INFO - Error code 2 was found, retry 2
2020-02-14 14:13:31,529 - root - INFO - Get not expected error code 1
2020-02-14 14:13:31,529 - root - INFO - call RetryWhileForJson(DoHttpRequest2)(...)
2020-02-14 14:13:31,529 - root - INFO - Get not expected error code 1
Other useful retry functions:
This package provide two scenarios of failure retries
- Until scene
- RetryUntilForJson()
- RetryUntilForBool()
- while scene
- RetryWhileForJson()
- RetryWhileForBool()
Metadata
Release files for tinyretry 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tinyretry-0.1.4.tar.gz | 3.0 kB | Without provenance | Details for tinyretry-0.1.4.tar.gz |
Release files / tinyretry-0.1.4.tar.gz
| Download URL | tinyretry-0.1.4.tar.gz |
|---|---|
| Size | 3.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6e0c5ec4c9c465e76b23e93b82569372ab71b29d89fc8975ce41b12df454b382
|
|
BLAKE2b-256 checksum How to use checksums |
789170dd1a695486c14b9a8af75a7ec21dcb7125522bc079a8f3a3ce4f05bf6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/2.7.15
|
Security for tinyretry 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.