Skip to main content

⏲️ Rate-limiting, thread-safe and asynchronous decorators + 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.

Installation

Requirements

  • Python 3.7+

Installing from PyPI

pip3 install limiter

Usage

from asyncio import sleep as aiosleep
from time import sleep as tsleep

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:
    tsleep(1)


@limit(limiter, consume=2)
async def do_stuff():
    await aiosleep(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

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.

Files for limiter, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size limiter-0.1.2-py2.py3-none-any.whl (15.2 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size limiter-0.1.2.tar.gz (15.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page