Skip to main content

Debounce and throttle decorators for Python functions.

Project description

philiprehberger-debounce

Debounce and throttle decorators for Python functions.

Install

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.

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.0.tar.gz (3.7 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.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_debounce-0.1.0.tar.gz
  • Upload date:
  • Size: 3.7 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.1.0.tar.gz
Algorithm Hash digest
SHA256 d0f0c2178cc75df3f4ae10254391b552b8faeeea03b921818b780e0f18501c51
MD5 37e19ad5568bce4ef068e4ca2fc1ee79
BLAKE2b-256 190eea9abc965c90d535a2a093c0644cdbe5b85c5555fb5a80228691b8fb02a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_debounce-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 397f55c8ae1d0e52422d5a68abd5ee6c7edcc6315b20c0af0be00f315cfdcae5
MD5 1891aea9767e6a6117079a7648854fde
BLAKE2b-256 f2579d27f7a1fbec3e905b43e6db098dd63bb4c5454b9dc2c7b0dff3dbd7304c

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