Skip to main content

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

Project description

⏲️ 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.post0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: limiter-0.2.0.post0.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.post0.tar.gz
Algorithm Hash digest
SHA256 6e21313a9bb0097a7f341f2675efc2511807af12f204ae7d9e659f574ce17ea1
MD5 cd2320777639338dde3d3a12f630f35c
BLAKE2b-256 5f16deacb11ef282730781636ce34e8b335a6628895cf1bea4072d5a190329af

See more details on using hashes here.

File details

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

File metadata

  • Download URL: limiter-0.2.0.post0-py2.py3-none-any.whl
  • Upload date:
  • Size: 16.0 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.post0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fabaf8b0a64cb75650406354ab24b6bdbb3d634fd448b4930b380f30bb9d38d8
MD5 cd560b292ba8a953cd4cd719752005b2
BLAKE2b-256 70393feadcf70d00c1af2185ea84d4c24c1f9ce72bdbebccd9430042ae4b1392

See more details on using hashes here.

Supported by

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