Requests Ratelimiter
Requests-Ratelimiter is a Python library that provides easy-to-use rate-limiting capabilities for the popular `requests` library. It acts as a wrapper around `pyrate-limiter`, implementing the leaky bucket algorithm to control the rate of HTTP requests. It is currently at version 0.9.3 and generally follows the release cadence of its underlying `pyrate-limiter` dependency, with updates typically driven by new features or fixes in the core rate-limiting logic.
Warnings
- breaking Dropped support for Python 3.8 and 3.9 as of version 0.9.0, aligning with upstream `pyrate-limiter` requirements. Requires Python >=3.10.
- gotcha By default, each `LimiterSession` (and `LimiterAdapter`) operates independently, tracking rate limits only within its own instance. In multi-threaded environments, multiple processes, or web applications, this can lead to exceeding actual server-side rate limits.
- gotcha When combining `requests-ratelimiter` with other `requests`-based libraries (e.g., `requests-cache`) using mixins, the inheritance order of mixin classes is crucial. If rate-limiting is applied *before* caching, cache hits will still count against your rate limit.
- breaking In earlier versions, `LimiterSession` accepted a `rates` argument to define rate limits. This was replaced by direct `per_second`, `per_minute`, etc., arguments, or by passing a `Limiter` object. Using the old `rates` argument will raise an `InvalidParams` exception from `pyrate_limiter`.
- gotcha The `per_host` parameter, available for both `LimiterSession` and `LimiterAdapter`, enables automatic tracking of rate limits separately for each host. If not enabled, a single rate limit applies across all hosts, which might not be the desired behavior for APIs with per-domain limits.
Install
-
pip install requests-ratelimiter
Imports
- LimiterSession
from requests_ratelimiter import LimiterSession
- LimiterAdapter
from requests_ratelimiter import LimiterAdapter
- LimiterMixin
from requests_ratelimiter import LimiterMixin
- RequestRate
from pyrate_limiter import RequestRate
- Duration
from pyrate_limiter import Duration
Quickstart
import os
from requests_ratelimiter import LimiterSession
from time import time
session = LimiterSession(per_second=5) # Limit to 5 requests per second
start = time()
for i in range(10):
try:
response = session.get('https://httpbin.org/get') # Or any other API endpoint
response.raise_for_status()
print(f'[t+{time()-start:.2f}] Sent request {i+1} (Status: {response.status_code})')
except Exception as e:
print(f'Request {i+1} failed: {e}')
# Example with custom rate using pyrate-limiter objects
from pyrate_limiter import RequestRate, Duration, Limiter
# Limit to 10 requests per minute
custom_limiter_session = LimiterSession(limiter=Limiter(RequestRate(10, Duration.MINUTE)))
start_custom = time()
for i in range(5):
try:
response = custom_limiter_session.get('https://httpbin.org/get')
response.raise_for_status()
print(f'[t+{time()-start_custom:.2f}] Sent custom-limited request {i+1} (Status: {response.status_code})')
except Exception as e:
print(f'Custom-limited request {i+1} failed: {e}')