Skip to main content

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

Project description

limiter

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

  • Thread-safe, with no need for a timer thread
  • Control burst requests
  • Control average request rate
  • Easy to use

Installation

Requirements

  • Python 3.7+

Installing from PyPI

pip3 install limiter

Usage

from asyncio import sleep

from requests import get, Response
from limiter import get_limiter, limit


REFRESH_RATE = 2
BURST_RATE = 3


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


@limit(limiter)
def get_page(url: str) -> Response:
    return get(url)


@limit(limiter, consume=2)
async def do_stuff():
    await sleep(0.1)


def do_stuff():
    # do stuff

    with limit(limiter, consume=1.5):
        # do expensive stuff
        pass


async def do_stuff():
    # do stuff

    async with limit(limiter):
        # do expensive stuff
        pass


@limit(limiter, bucket=b'other stuff')
def do_other_stuff():
    pass

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.1.0.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distributions

limiter-0.1.0-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

limiter-0.1.0-py2.py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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