pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

[Feature Request] Throttle Decorator

Open tal opened this issue 3 years ago • 2 comments

I've found ways to implement throttle myself in a few ways but I think it'd be nice if the framework supplied its own.

I could also see it taking in a string for uniqueness like task.unique.

Thanks!

tal avatar Sep 19 '21 18:09 tal

Perhaps you can share your throttle code as an example of how you'd use this functionality?

dlashua avatar Oct 30 '21 12:10 dlashua

throttle_last_run_at = {}
def should_throttle(key, interval):
    now = time.time()
    last_run = throttle_last_run_at.get(key, 0)
    if now - last_run < interval:
        return True
    else:
        throttle_last_run_at[key] = now
        return False

@event_trigger("deconz_event", "unique_id == '00:15:8d:00:05:89:13:89'")
def kids_round_switch_pressed(event=None, **kwargs):
    if should_throttle('fuss_go', 1):
        return

tal avatar Jan 07 '22 03:01 tal