Skip to main content

A simple function decorator that enables rate limiting using fixed window or sliding window strategies.

Project description

ratelimiter

A small Python library that provides simple function decorators for rate limiting using either a fixed-window (bursty) or a sliding-window (steady) strategy.

Example — Fixed window (bursty)

from ratelimits import ratelimits
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime as dt

@ratelimits("fixed_window", calls=10, period=20)
def my_task(x):
    print(f"{dt.now()} | task", x)

with ThreadPoolExecutor(max_workers=None) as executor:
    executor.map(my_task, range(20))

Example — Sliding window (steady, evenly spaced)

from ratelimits import ratelimits
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime as dt

@ratelimits(
  type   = "sliding_window", 
  calls  = 10, 
  period = 60,
  debug  = True,
  log_message = lambda args, kwargs: f"Running with args {args} and kwargs {kwargs}"
)
def my_task(x):
    pass

with ThreadPoolExecutor(max_workers=None) as executor:
    executor.map(my_task, range(20))

Parameters Summary

Parameter Type Description
calls int Maximum number of calls allowed per window.
period int | float Duration of the window in seconds.
offset_start / offset_end (FixedWindow only) Optional adjustments for the start and end edges of the window.
debug bool Enables verbose logging to stdout for debugging and tracing sleep intervals.
log_message text Optional print statement to track execution parameters.

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

ratelimits-0.1.4.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

ratelimits-0.1.4-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file ratelimits-0.1.4.tar.gz.

File metadata

  • Download URL: ratelimits-0.1.4.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for ratelimits-0.1.4.tar.gz
Algorithm Hash digest
SHA256 867ee20493483cf3f16cad43e7aeb6c0eefa9482630ed0928430b87bf20f46b9
MD5 a62cbbfd377ea5aecb4bd0da0374ca96
BLAKE2b-256 06c04b0d741d9cf644220309af4414e7dde51dbcd7fa9f506de3c620be6215ed

See more details on using hashes here.

File details

Details for the file ratelimits-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: ratelimits-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for ratelimits-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3acfd0e13e01be9bb572dceb15d2dd0037c1c699eaa0892764ed0ea613297957
MD5 5703c705163061ddd92e78d73655611a
BLAKE2b-256 8f3fa07a0555cdc4e2f4436bc5e2d6b33c851662dd2ca6d7b71d982e6be236d3

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