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):
    print(f"[{dt.now()}] task", x)

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.3.tar.gz (4.7 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.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ratelimits-0.1.3.tar.gz
  • Upload date:
  • Size: 4.7 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.3.tar.gz
Algorithm Hash digest
SHA256 ab9ec0d502c31097169b8eb97b76c604d0a18b0d47c27c12ca1fc5f8013edbc9
MD5 a09a08e4bea24e8d75c3ed8b3d82025e
BLAKE2b-256 073c7fabbfb5722bf81a62062cda661b1000ab9290a827b326cdaa0c25e02904

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ratelimits-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.8 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3ff4f80ce090da61ecc8daeffc36c57d7bbbb8385d9f35121df78fe8c26db843
MD5 689650625029a27089865eed318a42b1
BLAKE2b-256 0c0d2e21cef936993abdac44c2a1b27830d628f18deb34baf44b21bde313bdcc

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