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("sliding_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))

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.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: ratelimits-0.1.2.tar.gz
  • Upload date:
  • Size: 4.6 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.2.tar.gz
Algorithm Hash digest
SHA256 a377339199b500a4208376c8202cf61fefe4618994101306ad34bd880eb9fed2
MD5 b080a2d25eef65f22d7e377b0089607b
BLAKE2b-256 74e9f9ef1617a308796c407694faf0931bb9c741b1f89746b03916d394b19bfd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ratelimits-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fc998c04151d7b135e1e2db06cad2e32daa8c9f564227535df5f01b684c550fb
MD5 5de2ee0721a6b875f896e571b8cf6070
BLAKE2b-256 3140cade5ff3c9179935509737872b89046fa847c3f691f189105f9c3e8eb90c

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