Skip to content

Throttling & Debouncing#

These utilities allow you to throttle or debounce a function. This is useful when you have a function that is called multiple times in a short period of time, and you want to make sure it is only "actually" called once (or at least no more than a certain frequency).

For background on throttling and debouncing, see:

superqt.utils.qdebounced(func=None, timeout=100, leading=False, timer_type=Qt.TimerType.PreciseTimer, parent=None) #

Creates a debounced function that delays invoking func.

func will not be invoked until timeout ms have elapsed since the last time the debounced function was invoked.

The debounced function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them. Options indicate whether func should be invoked on the leading and/or trailing edge of the wait timeout. The func is invoked with the last arguments provided to the debounced function. Subsequent calls to the debounced function return the result of the last func invocation.

This decorator may be used with or without parameters.

Parameters:

Name Type Description Default
func Callable

A function to throttle

None
timeout int

Timeout in milliseconds to wait before allowing another call, by default 100

100
leading bool

Whether to invoke the function on the leading edge of the wait timer, by default False

False
timer_type TimerType

The timer type. by default Qt.TimerType.PreciseTimer One of: - Qt.PreciseTimer: Precise timers try to keep millisecond accuracy - Qt.CoarseTimer: Coarse timers try to keep accuracy within 5% of the desired interval - Qt.VeryCoarseTimer: Very coarse timers only keep full second accuracy

PreciseTimer
parent QObject | None

Parent object for timer. If using qthrottled as function it may be usefull for cleaning data

None

superqt.utils.qthrottled(func=None, timeout=100, leading=True, timer_type=Qt.TimerType.PreciseTimer, parent=None) #

Creates a throttled function that invokes func at most once per timeout.

The throttled function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them. Options to indicate whether func should be invoked on the leading and/or trailing edge of the wait timeout. The func is invoked with the last arguments provided to the throttled function. Subsequent calls to the throttled function return the result of the last func invocation.

This decorator may be used with or without parameters.

Parameters:

Name Type Description Default
func Callable

A function to throttle

None
timeout int

Timeout in milliseconds to wait before allowing another call, by default 100

100
leading bool

Whether to invoke the function on the leading edge of the wait timer, by default True

True
timer_type TimerType

The timer type. by default Qt.TimerType.PreciseTimer One of: - Qt.PreciseTimer: Precise timers try to keep millisecond accuracy - Qt.CoarseTimer: Coarse timers try to keep accuracy within 5% of the desired interval - Qt.VeryCoarseTimer: Very coarse timers only keep full second accuracy

PreciseTimer
parent QObject | None

Parent object for timer. If using qthrottled as function it may be usefull for cleaning data

None

superqt.utils.QSignalDebouncer #

Bases: GenericSignalThrottler

A Signal Debouncer.

This object's triggered signal will not be emitted until self.timeout() milliseconds have elapsed since the last time triggered was emitted.

superqt.utils.QSignalThrottler #

Bases: GenericSignalThrottler

A Signal Throttler.

This object's triggered signal will emit at most once per timeout (set with setTimeout()).

superqt.utils._throttler.GenericSignalThrottler #

Bases: QObject

cancel() #

Cancel any pending emissions.

emissionPolicy() #

Return the emission policy (trailing or leading).

flush() #

Force emission of any pending emissions.

kind() #

Return the kind of throttler (throttler or debouncer).

setTimeout(timeout) #

Set timeout in milliseconds.

setTimerType(timerType) #

Set current Qt.TimerType.

throttle() #

Emit triggered if not running, then start timer.

timeout() #

Return current timeout in milliseconds.

timerType() #

Return current Qt.TimerType.