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.2.0.tar.gz (5.6 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.2.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_debounce-0.2.0.tar.gz
Algorithm Hash digest
SHA256 533c3b6d944f70bb25833a16a0b4733ff11d3230d8628fb2a59fe19e4a06205e
MD5 a66c32eb7d324738553cf5bf6bf76fe3
BLAKE2b-256 2d38c2e8a0dac0b3bbd0cf99eb616c2687e424e618a037a2425d223d01f43444

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_debounce-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9779a7e22cc9bdd1571e4471fd4fb2f6fed62664065c81305f99c1c273afa5a1
MD5 a10c24fe9e9ee51e7f11448ba2235ba8
BLAKE2b-256 8f72e4849b418c8bbea58947971f2a7711c6c7f0ee21a437ee9c43192149786b

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