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

Bounded debounce

Use max_wait to guarantee the function fires at most max_wait seconds after the first pending call, even if calls keep arriving and continuously reset the debounce timer. Mirrors lodash debounce({ maxWait }) semantics.

@debounce(0.5, max_wait=2.0)
def autosave(content):
    print(f"Saving: {content[:20]}...")

# Even with continuous edits, autosave fires at least every 2s.
for chunk in stream_keystrokes():
    autosave(chunk)

max_wait must be positive and >= seconds; otherwise ValueError is raised.

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 Parameter Description
debounce(seconds, *, leading=False, max_wait=None) seconds Minimum quiet period (in seconds) before the function is invoked. Each new call cancels the previous pending invocation and restarts the timer.
leading If True, fire on the leading edge of the window instead of the trailing edge. Subsequent rapid calls are suppressed until seconds of silence have elapsed.
max_wait Optional upper bound (in seconds) on how long the function may be deferred from the first pending call. Must be positive and >= seconds. Mirrors lodash debounce({ maxWait }).
throttle(calls, per) calls Maximum number of allowed invocations within the sliding window.
per Length of the sliding window in seconds. Calls beyond calls within per are silently dropped.

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.3.0.tar.gz (7.0 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.3.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_debounce-0.3.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_debounce-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f819fcdebc878c31d9ef162f72e45a38eaeb315f0c9df6657bc9833d3287fc94
MD5 ebef09d8061cb1016d60d05337555375
BLAKE2b-256 7fa94c365cd682d06f36d6548108bd041cc5c7bb38bfd6e33fe1aaca4175c13a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_debounce-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ecebe245095a2d2f1f9ea0941c36521b0e65ee134c6601cee4d09bb748b1e6d2
MD5 4d05fa08aea2f115185553b75baa80bb
BLAKE2b-256 6f65ebcede97bc2fafb064bb3433b32a5c5516218aa08e503950dba7e497b886

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