Ratelimiting for httpx
Project description
httpx-ratelimit
Simple wrapper around PyrateLimiter that adds integration with httpx library
Usage
Using per_* kwargs:
from time import time
from httpx import Client
from httpx_ratelimiter import LimiterTransport
# Apply a rate limit of 5 requests per second to all requests
# if wait time between request will be more than 600ms, raise an BucketFullException
with Client(transport=LimiterTransport(per_second=5, max_delay=600)) as c:
start = time()
for _i in range(100):
print(
f'[t + {time() - start: .2f}] got response: {c.get("https://httpbin.org/status/200,429")}'
)
Using custom limiter object
from time import time
from httpx import Client
from httpx_ratelimiter import LimiterTransport
from pyrate_limiter import Limiter, Rate, Duration
# limit 5 requests over 2 seconds and 10 requests over 1 minute
rates = [Rate(5, Duration.SECOND * 2), Rate(10, Duration.MINUTE)]
# if wait time between request will be more than 600ms, silently fail
limiter = Limiter(rates, max_delay=600, raise_when_fail=False)
with Client(transport=LimiterTransport(limiter=limiter)) as c:
start = time()
for _i in range(100):
print(
f'[t + {time() - start: .2f}] got response: {c.get("https://httpbin.org/status/200,429")}'
)
Thanks
Thank to original Requests-ratelimiter author for idea and backbone of a project.
Project details
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
Close
Hashes for httpx_ratelimiter-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 058f8ef742818b73979330fb93e28e931653f4ab41bc91a3ae5b79deb0368d67 |
|
MD5 | e74c59075ae28cde6ff76d832e73b5a4 |
|
BLAKE2b-256 | d2185f4a4b58cb39c61d899ee41bb4afda431338fc0c7c0a52840dc59c370f6e |