choke: simple implementation of throttling mechanism.
Project description
choke is a package implementing a trivial to use general purpose throttling mechanism. The basic workflow with choke is as follows:
Create manager - an object responsible for keeping track of timestamps when some events (think: calls to your functions) occur.
Instruct manager to “choke” some callables, i.e. define maximum number of calls that can occur per given time window.
Use your callables as usual, keeping in mind that when the above defined limit is exceeded, the choked callable will raise CallLimitExceededError.
Here is an example containing everything you need to use choke:
from time import sleep
from redis import StrictRedis
from choke import RedisChokeManager, CallLimitExceededError
REDIS = StrictRedis() # Tweak this to reflect your setup
CHOKE_MANAGER = RedisChokeManager(redis=REDIS)
# Example configuration: enforce limit of no more than 10 calls in two seconds window
@CHOKE_MANAGER.choke(limit=10, window_length=2)
def foo(x, y):
"""Just print something to show that foo was called."""
print(f'foo called with ({x}, {y})')
if __name__ == '__main__':
# We expect pattern of 10 successes followed by 10 failures followed again by 10 successes
# Some deviations from this pattern may obviously occur as calling foo takes nonzero time
for i in range(30):
try:
foo(i, y=i ** 2)
except CallLimitExceededError:
print('Foo not called. Limit exceeded!')
sleep(0.1)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file choke-0.1.0.tar.gz
.
File metadata
- Download URL: choke-0.1.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3721be300780efc5b0d1f8567ca8ee7c6c2807e1c03f2455a9e935f331cdecd1 |
|
MD5 | 2f25ec3dbcfa079fafebae85ac1bee61 |
|
BLAKE2b-256 | 55116b4566dbfb7940e65ae729f2631eabd9b16cd783e98390843be985878181 |