Skip to main content

throttlebounce

Small, typed, zero-dependency sync debounce and throttle decorators with an injectable clock.

Install

pip install throttlebounce

Usage

from throttlebounce import debounce, throttle


@debounce(0.3)
def save(text: str) -> None:
    write_to_disk(text)


# Called on every keystroke, it writes once, 0.3s after typing stops.
save("h")
save("he")
save("hello")        # only this one reaches disk


@throttle(1.0)
def ping() -> None:
    send_heartbeat()


# Called in a tight loop, it fires at most once per second.
while running:
    ping()

debounce collapses a burst of calls into a single run that happens once the calls go quiet (trailing edge), with the last call's arguments. The debounced callable returns None; the real run happens later on a background timer. It exposes flush() to run the pending call now and return its result, cancel() to drop it, and a pending flag.

throttle runs the function at most once per interval (leading edge): the first call runs, and calls inside the interval are skipped and return the last result. reset() clears the window.

Both decorators are thread-safe. Both take an injectable dependency so tests run without real waits: throttle(interval, clock=...) and debounce(wait, timer_factory=...).

class FakeClock:
    def __init__(self) -> None:
        self.now = 0.0

    def __call__(self) -> float:
        return self.now


clock = FakeClock()


@throttle(1.0, clock=clock)
def tick() -> int:
    return 1


assert tick() == 1       # first call runs
clock.now = 0.5
assert tick() == 1       # skipped, returns the last result, no real sleep

Why

Python's standard library has no debounce or throttle, the timing helpers that JavaScript developers reach for constantly. The packages that exist fall short:

  • throttle has not been released since 2020 and is effectively abandoned.
  • throttler is an asyncio rate limiter, not a sync decorator, and does not debounce.
  • call-throttle throttles but has no debounce.
  • python-debouncer debounces but pulls in a runtime dependency and has not been touched since 2024.

None of them offer a clean, maintained, typed, zero-dependency pair of sync debounce and throttle decorators that are thread-safe and testable without real time. That is the gap this fills. Reach for it in desktop apps, CLIs, background workers and GUIs: anywhere a function is triggered more often than you want it to run.

License

MIT. See LICENSE.

Metadata

Release files for throttlebounce 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for throttlebounce 0.1.0
File Size Uploaded
throttlebounce-0.1.0.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for throttlebounce 0.1.0
File Interpreter ABI Platform
throttlebounce-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / throttlebounce-0.1.0.tar.gz

Download URL throttlebounce-0.1.0.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a03d3ffe6f27d468b6bf6a0b41d5812dfcc9e54ef9ad461b2c02cee62d85b847
BLAKE2b-256 checksum
How to use checksums
15a9fffcaa9ecf30bea2064431646355b41a06b82912fcf7f4c177cffdbd4368
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / throttlebounce-0.1.0-py3-none-any.whl

Download URL throttlebounce-0.1.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
68b4f67b343242b0654613f753ee3c64ac8cecd5246935215601077c29cdded5
BLAKE2b-256 checksum
How to use checksums
526eec40d1d1c9fb92818ef1fdd749000b1dac6a02359ec4e181a2536713b3b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page