PyrateLimiter

4.1.0 · active · verified Thu Apr 09

PyrateLimiter is a Python library implementing the Leaky-Bucket Algorithm for rate limiting. It supports both synchronous and asynchronous workflows and offers various backends like in-memory, SQLite, Redis, and PostgreSQL for persistent limit tracking. Currently at version 4.1.0, it maintains an active development and release cadence, with several minor and patch releases in the last year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic in-memory rate limiter with a single rate. It shows both blocking and non-blocking acquisition attempts.

from pyrate_limiter import Duration, Rate, Limiter

# Limit 5 requests within 2 seconds
limiter = Limiter(Rate(5, Duration.SECOND * 2))

# Blocking mode (default) - waits until permit available
for i in range(6):
    limiter.try_acquire(str(i))
    print(f"Acquired permit {i}")

# Non-blocking mode - returns False if bucket full
for i in range(6):
    success = limiter.try_acquire(str(i), blocking=False)
    if not success:
        print(f"Rate limited at {i}")

view raw JSON →