Timeout & retry functions in Python with a single line of code
Project description
Python Timeout
Timeout & retry functions in Python with a single line of code
Explore the docs »
·
Report Bug
·
Request Feature
·
Table of Contents
About The Project
This is a Python package that provides decorators for adding timeout and retry functionality to your functions. With just one line of code, you can easily set a timeout for your function and choose to retry it any number of times in case of timeout.
Getting Started
This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.
Installation
Build from source
- Clone the repository
git clone github.com/radioactive11/python-timeout
- Run the following command to install the project
python3 setup.py install
Install using pip
To install the function using pip, run
pip3 install python-timeout
Usage
Basic Usage
-
To set timeout for a function, import the module using
from libtimeout import Timeout
. -
Create a Timeout object class with the following parameters:
`timeout_limit`: The time limit for the function to run (in seconds). Default is 10 seconds. `retry_limit`: The number of times to retry the function in case of timeout. Deafult is 0.
-
Use the
bind
method to bind the timeout decorator to the function you want to set timeout for. This returns a new function with the timeout decorator applied. -
Call the new function with the same parameters as the original function.
Example
from libtimeout import Timeout
timeout_ = Timeout(timeout_limit=5, retry_limit=2)
@timeout_.bind()
def my_function(a, b):
# Do something
return a + b
my_function(1, 2)
Advanced Usage
By default, the timeout decorator raises a TimeoutError in case of timeout. You can change this behavior by passing the following parameters:
timeout_exception: The exception to raise in case of timeout. This must be a subclass of Exception.
timeout_handler: The function to call in case of timeout. This must raise the exception specified in timeout_exception.
Example
from libtimeout import Timeout
class CustomException(Exception):
pass
def custom_handler():
# do something like deleting partial downloaded files etc.
raise CustomException("Custom exception raised")
timeout_ = Timeout(timeout_limit=60, retry_limit=2, timeout_exception=CustomException, timeout_handler=custom_handler)
@timeout_.bind()
def download_large_file(url):
# Download file
return
Roadmap
- [] Add retry with backoff
- [] Add multi-threading support
See the open issues for a full list of proposed features (and known issues).
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE.txt
for more information.
Contact
Arijit Roy - @radioactive11 - roy.arijit@icloud.com
Project Link: https://github.com/your_username/repo_namehttps://github.com/radioactive11/python-timeout
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file pyrtout-1.0.3.tar.gz
.
File metadata
- Download URL: pyrtout-1.0.3.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b835be53a76287ba1a5a9ad213caac19864e6c611e217496174d91f33f313db4 |
|
MD5 | bf538717e8fe8ae18a0ec6570241e031 |
|
BLAKE2b-256 | 46621455ba71e9f114b9acb673b00fa5cadd4d3fa6e93bccb5b75ee454490c79 |
File details
Details for the file pyrtout-1.0.3-py3.8.egg
.
File metadata
- Download URL: pyrtout-1.0.3-py3.8.egg
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9290dec03b9a1a9772b21478ca7b1ef27ed24f4e54346e1faa363f6dc68720d3 |
|
MD5 | c0d7269f7a6c7075395e4dd0b4e354c1 |
|
BLAKE2b-256 | bebbafd798616681fecb5b9fc667718f6da99b4630ebb0633f65e670d4122ed3 |
File details
Details for the file pyrtout-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: pyrtout-1.0.3-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83a76b4ddfd6c4c8e788ba2c7620a983de8c40bffaa9ba1cf7772028f923ee69 |
|
MD5 | 0299c4420493f4a8d465f9c4697ad677 |
|
BLAKE2b-256 | 72897cdbad52d6910a7da5eb98a92559a753834f71cd69f5cb4e26a899c15f0d |