Skip to main content

throttlebounce

Lianes8 Certified

Typed, zero-dependency debounce and throttle decorators for Python, sync and async, with the edge semantics of lodash and Underscore and injectable time, so your tests never sleep.

  • debounce: run once a burst of calls goes quiet (trailing edge), on its first call (leading edge), or both, with an optional max_wait so a non-stop stream still runs.
  • throttle: run at most once per interval, optionally also the last call of a busy window (trailing edge).
  • async_debounce and async_throttle: the same for coroutine functions, on the running event loop.
  • A throttlebounce command that debounces or throttles a shell command fed by trigger lines on standard input.

Install

pip install throttlebounce

Python 3.10 or newer. No runtime dependencies.

Quick start

from throttlebounce import debounce, throttle

saved = []


@debounce(0.3)
def save(text: str) -> None:
    saved.append(text)


# Called on every keystroke; only the last call runs, 0.3 s after typing stops.
save("h")
save("he")
save("hello")
save.flush()  # run the pending call now instead of waiting
assert saved == ["hello"]


@throttle(1.0)
def progress(percent: int) -> int:
    return percent


assert progress(10) == 10  # the first call runs at once
assert progress(20) == 10  # inside the interval: skipped, returns the last result

Every wrapper has flush() (run the pending call now), cancel() (drop it), pending and result; throttles also have reset(). The sync wrappers are thread-safe, and the wrapped function always runs outside their lock.

How the edges work

Call Runs
debounce(w) once, w seconds after the last call of a burst, with that call's arguments
debounce(w, leading=True, trailing=False) on the first call of a burst; a burst ends after w seconds without calls
debounce(w, leading=True) on the first call, and again at the end only if more calls came
debounce(w, max_wait=m) as above, and during an unbroken burst at most m seconds after the burst began or after the last run
throttle(i) the first call, then the first call after each i-second window
throttle(i, trailing=True) as above, and the last call made inside a window runs when that window closes

These rules are checked against lodash 4.18.1 and Underscore 1.13.8 on 1200 seeded call patterns, run live on a virtual clock. On plain calls the only differences are two places where lodash runs a call later than its own deadline, or twice inside one throttle interval; flush() and cancel() also differ by design. The guide has the details.

Async

import asyncio

from throttlebounce import async_debounce

sent = []


@async_debounce(0.3)
async def push(value: int) -> None:
    sent.append(value)


async def main() -> None:
    push(1)
    push(2)
    await push.flush()


asyncio.run(main())
assert sent == [2]

Methods

A decorated method keeps separate state for each object, so two editors never share a pending save:

from throttlebounce import debounce


class Editor:
    def __init__(self) -> None:
        self.saved = []

    @debounce(0.5)
    def save(self, text: str) -> None:
        self.saved.append(text)


a, b = Editor(), Editor()
a.save("one")
b.save("two")
a.save.flush()
b.save.flush()
assert (a.saved, b.saved) == (["one"], ["two"])

Testing without real time

Pass a clock to the throttles, a timer_factory to debounce and throttle(trailing=True), and a scheduler to the async pair, and drive time by hand:

from throttlebounce import throttle

now = 0.0


@throttle(1.0, clock=lambda: now)
def tick() -> str:
    return f"ran at {now}"


assert tick() == "ran at 0.0"
now = 0.5
assert tick() == "ran at 0.0"  # skipped
now = 1.0
assert tick() == "ran at 1.0"

Command line

Each line on standard input is a trigger; the command after -- runs on the chosen schedule. When the input ends a pending run happens at once, and the exit status is that of the last run.

$ printf 'a\nb\nc\n' | throttlebounce debounce --wait 0.5 -- python -c "print('rebuilt')"
rebuilt

Typical uses: watch-files | throttlebounce debounce --wait 0.5 -- make build, or tail -f events.log | throttlebounce throttle --interval 10 --trailing -- ./deploy.sh. Run throttlebounce --help for every flag.

Why

The standard library has no debounce or throttle. On PyPI, as of October 2026:

throttlebounce puts both in one small typed package, sync and async, with leading and trailing edges and max_wait that behave as in lodash and Underscore (and a test suite that runs those libraries to prove it), per-instance methods, and time you can inject.

Documentation

  • Guide: every option, the control methods, threads, async, testing, the command line, and the differences from lodash.
  • Changelog

License

MIT. See LICENSE.

Metadata

Release files for throttlebounce 1.0.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 1.0.0
File Size Uploaded Provenance Actions
throttlebounce-1.0.0.tar.gz 110.0 kB Without provenance Details for throttlebounce-1.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 130.6 kB

Release files / throttlebounce-1.0.0.tar.gz

Download URL throttlebounce-1.0.0.tar.gz
Size 110.0 kB
Tags Source
SHA-256 checksum
How to use checksums
aba5e0450be2630dba791e398ea550a77f8afb4e0308242a3b2d6398a8cca7ec
BLAKE2b-256 checksum
How to use checksums
6002836e166401add1a4c419ced9f1ccfa537bce84b423ec5217c6604036b142
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

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

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

Security for throttlebounce 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

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