Skip to main content

⏲️ Rate-limiting, thread-safe and asynchronous decorators + context managers that implement the token-bucket algorithm.

Project description

limiter: Easy rate limiting for Python

limiter makes it easy to add rate limiting to Python projects, using a token bucket algorithm. limiter can provide Python projects and scripts with:

Here are a few benefits of using limiter:

Usage

You can define dynamic and static limiters, and use them across your project.

Dynamic limit

You can define a limiter with a set rate and capacity. Then you can consume a dynamic amount of tokens from different buckets using limit():

from limiter import get_limiter, limit


REFRESH_RATE: int = 2
BURST_RATE: int = 3
MSG_BUCKET: bytes = b'messages'


limiter = get_limiter(rate=REFRESH_RATE, capacity=BURST_RATE)


@limit(limiter)
def download_page(url: str) -> bytes:
    ...


@limit(limiter, consume=2)
async def download_page(url: str) -> bytes:
    ...


def send_page(page: bytes):
    with limit(limiter, consume=1.5):
        ...


async def send_page(page: bytes):
    async with limit(limiter):
        ...


@limit(limiter, bucket=MSG_BUCKET)
def send_email(to: str):
    ...


async def send_email(to: str):
    async with limit(limiter, bucket=MSG_BUCKET):
        ...

Static limit

You can define a static limit and share it between blocks of code:

limit_downloads = limit(limter, consume=2)


@limit_downloads
def download_page(url: str) -> bytes:
    ...


@limit_downloads
async def download_page(url: str) -> bytes:
    ...


def download_image(url: str) -> bytes:
    with limit_downloads:
        ...


async def download_image(url: str) -> bytes:
    async with limit_downloads:
        ...

Installation

Requirements

  • Python 3.7+

Installing from PyPI

python3 -m pip install limiter

License

See LICENSE. If you'd like to use this project with a different license, please get in touch.

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

limiter-0.2.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

limiter-0.2.0-py2.py3-none-any.whl (15.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file limiter-0.2.0.tar.gz.

File metadata

  • Download URL: limiter-0.2.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for limiter-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5b309b2f7f1e8b9b563d8556a8d774fa42d8074e305a548f72d589598ae1a112
MD5 db7f78fd13570ec77b880b08a731710d
BLAKE2b-256 34e5982a8aadc3ae7c5703dd311eacec7d6f1b304de6445c9fb16d5e5254d39e

See more details on using hashes here.

File details

Details for the file limiter-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: limiter-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for limiter-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 781339f3ee0d4e1d958547641096438958f8b68878a7756d98e6f0a8e11ced5c
MD5 cbe22739e37ff9bfcd2318a2339b1e78
BLAKE2b-256 e45cfd2891c9947016c4d98906c84813d8d61428d021324edd64245f36952e16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page