Metadata-Version: 2.1
Name: timelimiter
Version: 0.1.1
Summary: Python time limiter library
Author-email: Keyes Hsu <keyes.hsu@gmail.com>
License: MIT License
        
        Copyright (c) 2022 Keyes Hsu
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/KeyesHsu/timelimiter
Project-URL: Bug Tracker, https://github.com/KeyesHsu/timelimiter/issues
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Provides-Extra: test
License-File: LICENSE

<h1 align="center">Welcome to timelimiter </h1>
<p>
  <img alt="PyPI" src="https://img.shields.io/pypi/v/timelimiter">
  <img alt="Python" src="https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9%20%7C%203.10-blue"/>
  <a href="https://github.com/KeyesHsu/timelimiter/blob/main/LICENSE" target="_blank">
    <img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" />
  </a>
  <a href="https://codecov.io/gh/KeyesHsu/timelimiter" > 
    <img alt="codecov" src="https://codecov.io/gh/KeyesHsu/timelimiter/branch/main/graph/badge.svg?token=OV7YT73BHY"/> 
 </a>
  <a href="https://github.com/KeyesHsu/timelimiter/actions/workflows/test.yml">
    <img alt="Tests" src="https://github.com/KeyesHsu/timelimiter/actions/workflows/test.yml/badge.svg?branch=main">
  </a>
</p>


Python time limit library, using event loop to schedule job.


##### Resources:
* [Changelog](https://github.com/KeyesHsu/timelimiter/blob/main/CHANGELOG.md)
* [PyPI](https://pypi.org/project/timelimiter/)


## Install

```sh
pip install timelimiter
```


## Usage
1. Extend `TimeoutHandler` with `timeout` expressed in seconds。Override `_run` method。`MySQLTimeoutHandler` for example.

```python
from timelimiter.timeout_handler import TimeoutHandler, TimeoutHandlerFactory


class MySQLTimeoutHandler(TimeoutHandler):
    timeout = 0.5

    def __init__(self):
        super(MySQLTimeoutHandler, self).__init__()
        # Some way to get MySQL thread id
        self.thread_id = 1

    def _run(self):
        # Kill MySQL connection
        print(self.thread_id)


class MySQLTimeoutHandlerFactory(TimeoutHandlerFactory):
    def create_handler(self) -> TimeoutHandler:
        return MySQLTimeoutHandler()
```

2. Use `TimeLimiter` to wrap the function。

```python
from timelimiter.event_loop import start_loop
from timelimiter.time_limiter import TimeLimiter

start_loop()
factory = MySQLTimeoutHandlerFactory()

@TimeLimiter(factory)
def foo():
    # Do something
    ...
```

## Configuration
Use environment to set configuration.

| Name                  | Description                         | Default |
|-----------------------|-------------------------------------|---------|
| TIME_LIMITER_CAPACITY | Max capacity for time limiter queue | 100,000 |


## Run tests

```sh
make test
```


## Author

👤 **Keyes Hsu**

* Github: [@KeyesHsu](https://github.com/KeyesHsu)

## 🤝 Contributing

Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](https://github.com/KeyesHsu/timelimiter/issues).

## Show your support

Give a ⭐️ if this project helped you!

## 📝 License

Copyright © 2022 [Keyes Hsu](https://github.com/KeyesHsu).<br />
This project is [MIT](https://github.com/KeyesHsu/timelimiter/blob/main/LICENSE) licensed.

***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_
