Skip to main content


Python Timeout

Timeout & retry functions in Python with a single line of code
Explore the docs »
· Report Bug · Request Feature ·

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact

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.

(back to top)

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

  1. Clone the repository
git clone github.com/radioactive11/python-timeout
  1. 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

(back to top)

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

(back to top)

Roadmap

  • [] Add retry with backoff
  • [] Add multi-threading support

See the open issues for a full list of proposed features (and known issues).

(back to top)

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Arijit Roy - @radioactive11 - roy.arijit@icloud.com

Project Link: https://github.com/your_username/repo_namehttps://github.com/radioactive11/python-timeout

(back to top)

Metadata

Release files for py-timelimit 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-timelimit 1.0.1
File Size Uploaded Provenance Actions
py-timelimit-1.0.1.tar.gz 4.6 kB Without provenance Details for py-timelimit-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-timelimit 1.0.1
File Interpreter ABI Platform Actions
py_timelimit-1.0.1-py3-none-any.whl Python 3 none any Details for py_timelimit-1.0.1-py3-none-any.whl

Total release size: 8.7 kB

Release files / py-timelimit-1.0.1.tar.gz

Download URL py-timelimit-1.0.1.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4e11807ee1a1dac6137c99db4dd234e2f73bf557b1ab572bb895ff01237df933
BLAKE2b-256 checksum
How to use checksums
e126e7aeeea1936732f6096e94b1c91c02caf1015fef18f6891c71a538a65587
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / py_timelimit-1.0.1-py3-none-any.whl

Download URL py_timelimit-1.0.1-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e38ae0b5b79e2aba0681d1b190d853947e6a6c491739a09315b07d054cd159a
BLAKE2b-256 checksum
How to use checksums
ddbeaf67de7149ab3fccd208321351f260391daece902b62a628a387f70c42c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Security for py-timelimit 1.0.1

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.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page