Skip to main content

An asynchronous rate limiter for Python, that automatically handles rate limits and retries. Useful for APIs having strict rate limits.

Project description

RateNinja

RateNinja is a Python package designed to facilitate the execution of function calls or API requests at a controlled rate, utilizing multithreading to maintain efficiency while adhering to rate limits. This package is particularly useful for interacting with APIs that impose call frequency restrictions, allowing developers to maximize throughput without violating terms of service.

Features

  • Rate Limiting: Enforces call frequency limits to comply with API rate limits.
  • Greedy and Non-Greedy Modes: Supports both evenly spaced calls and as-fast-as-possible call execution within rate limits.
  • Progress Bar: Visual feedback for tracking the progress of batched calls.
  • Retries: Automatic retry mechanism for failed calls.
  • Multithreading: Utilizes Python's concurrent.futures.ThreadPoolExecutor for parallel execution of calls within rate limits.

Installation

Install RateNinja using pip:

pip install rateninja

Usage

To use RateNinja, you need to import the RateNinja class from the package and then instantiate it with your desired rate limiting parameters. You can then pass the function (or API call) you wish to execute along with the function's arguments or keyword arguments.

Basic Example

from rateninja import RateNinja

# Function to be called
def calculate(param1, param2, operator="sum"):
    if operator == "sum":
        return param1 + param2
    elif operator == "multiplication":
        return param1 * param2
    else:
        raise Exception("Unrecognized operator")

# Initialize RateNinja with rate limiting parameters
rate_ninja = RateNinja(max_call_count=10, per_seconds=60, greedy=False, progress_bar=True, max_retries=5, max_workers=5)

# Prepare arguments for the function calls
func_args = [
    (3, 3),
    (4, 4),
    (5, 5),
] # List of tuples, each tuple contains arguments for one function call

func_kwargs = [
    {"operator": "sum"},
    {"operator": "multiplication"},
    {"operator": "ninja"},
]

# Execute the calls
results, errors = rate_ninja(my_function, func_args=func_args, func_kwargs=func_kwargs)

print("Results:", results)
print("Errors:", errors)

Should print:

Results: [6, 16, None]
Errors: [None, None, Exception("Unrecognized operator")]

Parameters

  • max_call_count: Maximum number of calls allowed per per_seconds period.
  • per_seconds: Time frame in seconds for the max_call_count limit.
  • greedy: If True, executes calls as fast as possible within the limit. If False, spaces out calls evenly (e.g. if max_call_count=10 and per_seconds=10, it will execute one call per second).
  • progress_bar: Enables a visual progress bar if True.
  • max_retries: Number of retries for failed calls.
  • max_workers: Number of worker threads for parallel execution.

Contributing

Contributions to RateNinja are welcome!

License

RateNinja is distributed under the Apache 2.0 License. See the LICENSE file in the GitHub repository for more details.

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

rateninja-1.0.0.tar.gz (9.1 kB view details)

Uploaded Source

File details

Details for the file rateninja-1.0.0.tar.gz.

File metadata

  • Download URL: rateninja-1.0.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for rateninja-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ec0d6aef64976a91958b2eb526cbd6f5d6e1e8ed706297f54c8de3287a5cfb29
MD5 7c60fc6e7640da30bd233718dd82c961
BLAKE2b-256 1d7bd8efff5d1bf33c3e95df7c70485a1f0b1f5bc16a067ea57195e3aa2903d4

See more details on using hashes here.

Provenance

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