A simple Python Ratelimiter
Project description
Rate limiter
Copyright (c) 2024 Tuyen Phan
A simple python implementation of a general purpose rate limiter.
Example:
from rate_limit import RateLimiter, Bucket, Period as Per
# Initialize.
rate_of = Bucket.builder()
limiter = RateLimiter(per_second=rate_of(3, Per.SECOND),
per_minute=rate_of(10, Per.MINUTE))
# Use limiter.
if limiter.reduce():
print "There is still quota left, so we can send your request."
else:
print "Quota exhausted for", list(limiter.exhausted())
# Display status.
print limiter.status()
# Get remaining tokens for bucket.
if limiter.get("per_minute") < 3:
print "You are awfully close to exhausting your minute limits."
There is also a cleaner way to utilize the rate limiter, by simply wrapping the implementation:
class Client(object):
def __init__(self, limiter):
self.send = limiter.wrap(self._send_impl)
def _send_impl(self, req):
print "Sending request (%s)" % json.dumps(req)
# This is now a rate-limited call which throws when exceeding the limit.
client.send("foo")
Also try the example use cases:
# A mock client with rate limiting.
python -u client.py
# The same client with retry attempts which eventually fail.
python -u retry_proxy.py
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple-ratelimiter-1.0.0.tar.gz.
File metadata
- Download URL: simple-ratelimiter-1.0.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
711be2904b912eaf733aaefd87c6728d0ae8edc0e3d4d917ba9611a9b1167a30
|
|
| MD5 |
ff686123d9cefa61e2a09f4157459fe9
|
|
| BLAKE2b-256 |
a5941e9e1adcbd43f60f3ede5b88bb38c0a22fca2c75a3752eb208b7bca3ff22
|
File details
Details for the file simple_ratelimiter-1.0.0-py3-none-any.whl.
File metadata
- Download URL: simple_ratelimiter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26e3cee4c54551153b322b4831d8423c89d419514137cd1c2a5c6d4ebf20bfd8
|
|
| MD5 |
37833d2be10d848da7b0509c5f3d4593
|
|
| BLAKE2b-256 |
8ca88879abd33c4678cd2f001e25e67ec9b60980c3a4af23121c13b9d3fb951a
|