Skip to main content

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

Project description

ratelimits

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.5.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.5-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ratelimits-0.1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 a13e517a8f0e15583d8399ff885788b568ec36fd56bf4419251fca763e66dc9d
MD5 8f771b66a70d26131c2f53c7d31ebf44
BLAKE2b-256 e4606f1753aacc86d15787043808d2c7a6f302506e2aa922f9e2363e491739c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ratelimits-0.1.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 46af5967618151dc6a061a24caecfe2003bc7413e9d9701b9825ec8a81af34be
MD5 29d3b97f5cf77c2ccee63a47389f9155
BLAKE2b-256 238b3ba19d6a4535d0636e64ff245a71edc0186e6ae2736b2154ce3d13b92b6f

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