Skip to main content

Yet Another Rate Limiter (for Python)

None

Project description

pyyarl

pyyarl (Yet Another Rate Limiter for Python) is a Python package for rate limiting function calls.

A lot of Python rate limiting packages seem to exist, but many of them do not have the desired behavior in many use cases. For example, many clear tasks only when the period has elapsed, rather than using a sliding window, or they may mark a task as done prior to running the function rather than after.

pyyarl uses a sliding window of completion times to enforce the rate limit, and is simple to use. pyyarl currently supports single and multithreaded execution.

Table of Contents

Quickstart

For example, the default Google Docs read request quota might be 300 requests per minute:

from pyyarl import rated_limited

@rate_limited(max_calls=300, period_s=60)
def make_read_request(file_id):
    ...  # some request to the Google Docs API


for file_id in file_ids:
    res = make_read_request(file_id)

Having multiple functions limited together

Sometimes, you want to limit multiple functions together such that their calls count towards the same rate.

from pyyarl import RateLimiter

rate_limiter = RateLimiter(max_calls=300, period_s=60)

@rate_limiter
def make_read_request_one(file_id) -> bool:
    ...  # some request to the Google Docs API


@rate_limiter
def make_read_request_two(file_id):
    ...  # some other request to the Google Docs API


for file_id in file_ids:
    if make_read_request_one(file_id):
        make_read_request_two(file_id)

Project details

None

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyyarl-1.0.0.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

pyyarl-1.0.0-py3-none-any.whl (4.3 kB view hashes)

Uploaded 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