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.

Source Distribution

limiter-0.1.2.tar.gz (15.0 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.1.2-py2.py3-none-any.whl (15.2 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: limiter-0.1.2.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5

File hashes

Hashes for limiter-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5608e4b6c0990b30a3477effe1fb48c969bf037963da689b946654944da4f997
MD5 1657b00450d89751cb704baa7da799aa
BLAKE2b-256 b2e12aa236d8d3f257515604ab66e5a13a59ca1b27fce8412848f17750702e90

See more details on using hashes here.

File details

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

File metadata

  • Download URL: limiter-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5

File hashes

Hashes for limiter-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 41f1ae4fa3a1f1d910c171939d166a90e63c6fff49c4b0aeb9db993f14b9cc4a
MD5 252bd256ad5cfee670e6f6f2cc2c104c
BLAKE2b-256 baa93f4e4b38f67c6bcfb4365b61e8ff576252b7219845b47d271ac0b2db26e2

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