Skip to main content

A Python library to rate limit functions and commands.

Project description

Function Rate Limiter

A simple Python library to rate limit function calls to a specific number of times per second/minute.

Installation

pip install .

Usage

You can use the @rate_limit decorator to easily limit how frequently a function is executed.

Blocking (Wait)

By default, the rate limiter will block and wait until the function is allowed to run.

import time
from function_rate_limiter import rate_limit

# Allow 2 calls per 5 seconds
@rate_limit(max_calls=2, period=5)
def print_hello():
    print(f"Hello at {time.strftime('%X')}")

for _ in range(5):
    print_hello()

Non-Blocking (Raise Exception)

If you don't want the function to block, set wait=False. It will raise a RateLimitExceeded exception instead.

from function_rate_limiter import rate_limit, RateLimitExceeded

@rate_limit(max_calls=2, period=5, wait=False)
def quick_task():
    print("Doing work...")

try:
    for _ in range(5):
        quick_task()
except RateLimitExceeded as e:
    print(f"Error: {e}")

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

function_rate_limiter-0.1.0.tar.gz (1.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

function_rate_limiter-0.1.0-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file function_rate_limiter-0.1.0.tar.gz.

File metadata

  • Download URL: function_rate_limiter-0.1.0.tar.gz
  • Upload date:
  • Size: 1.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for function_rate_limiter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 85a5a536e2f9ed27e510c352014d489aa9c7d52e45d33cc4947e771a69d394a5
MD5 4f86077c4da3b4488056e46ef4523b74
BLAKE2b-256 01d7b0ebbdf0db4a9ed128d8e3433634930cc56a36513ae73f92acea072760dc

See more details on using hashes here.

File details

Details for the file function_rate_limiter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for function_rate_limiter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb5c48024b2af734fd3458ea37bda317f15e77c10b0c3aa1c8f51d0e547038fa
MD5 ed83c7f373e951c6e6b51d62c5295c75
BLAKE2b-256 25aa702dc472db7946225e5683371771c37f171517592b1a029adc09ce9ab943

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