Welcome to timelimiter
Python time limit library, using event loop to schedule job.
Resources:
Install
pip install timelimiter
Usage
- Extend
TimeoutHandler
withtimeout
expressed in seconds。Override_run
method。MySQLTimeoutHandler
for example.
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()
- Use
TimeLimiter
to wrap the function。
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
make test
Author
- Github: @KeyesHsu
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Show your support
Give a
📝 License
Copyright © 2022 Keyes Hsu.
This project is MIT licensed.
This README was generated with