Skip to main content

Debounce and throttle decorators for Python functions.

Project description

philiprehberger-debounce

Tests PyPI version Last updated

Debounce and throttle decorators for Python functions.

Installation

pip install philiprehberger-debounce

Usage

from philiprehberger_debounce import debounce, throttle

Debounce

Delay execution until a quiet period has passed. Each new call resets the timer.

@debounce(0.5)
def on_resize(width, height):
    print(f"Resized to {width}x{height}")

on_resize(800, 600)
on_resize(1024, 768)  # cancels the previous call
# Only the last call executes after 0.5s

Throttle

Limit a function to a fixed number of calls within a time window. Excess calls are silently dropped.

@throttle(calls=3, per=1.0)
def send_request(data):
    print(f"Sending {data}")

for i in range(10):
    send_request(i)  # only the first 3 calls within 1s execute

API

Decorator Description
debounce(seconds) Delays execution until seconds have elapsed since the last call. Cancels any pending invocation on each new call.
throttle(calls, per) Limits the function to calls invocations per per seconds. Uses a sliding window.

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_debounce-0.1.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_debounce-0.1.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_debounce-0.1.2.tar.gz
Algorithm Hash digest
SHA256 413193397375400e8e88ca74042765ccd8c8a9239fb8779ad741ca00e128dc93
MD5 558b52358dd230336b4e644eccee1dd3
BLAKE2b-256 8a9a2a1bd3d017c330b860474c8123acef447995938a39adb63dbccee8cd6517

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_debounce-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 22df81b19d6c9fca32d1419f011134bb7216ebca3969f9a70b383f87c35b1880
MD5 d6b62a3cdfd8407311bf2010e7cb834e
BLAKE2b-256 20b1eb424808152f7e167e7733e3553a6c0056aec9d2257f237c20a07c24b924

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