10.17. timer

It is often required to repeatedly call a specific function or method with a certain time interval. This can be done using the class timer. See the example timerExample.py in the demo folder for what the timer can be used. The method or function can either be called once after a certain interval (single shot timer) or repeatedly with a given interval. In both cases, the variable that references the timer instance must always exist. Once it is destroyed, the timer is automatically stopped and deleted. Use the timer manager (menu scripts >> timer manager) to see a list of all active timer instances. This dialog also allows stopping or restarting these timers.

class itom.timer(interval, callbackFunc, argTuple=(), singleShot=False, name='', startAfterInit=True)

Creates a new timer object for (continously) triggering a callback function

Creates a timer object that (continuously) calls a python callback function or method. The timer is active right from the beginning, hence, after creating this object. If singleShot is True, the callback function is triggered once after the interval is passed (denoted as timeout). Else, the callback is continuously triggered with the given interval.

Please note, that the timer objects may time out later than expected if Python is currently busy or the operating system is unable to provide the requested accuracy. In such a case of timeout overrun, the callback function is only triggered once, even if multiple timeouts have expired, and then will resume the original interval.

An active timer can be stopped by the stop() method, or if this object is deleted. Furthermore, itom provides the Python Timer Manager dialog, where all or selected timers can be started or stopped.

New in itom 4.1: Added optional startAfterInit argument.

Parameters
intervalint

Time out interval in ms.

callbackFunccallable()

Python method (bounded) or function (unbounded) that should be called whenever the timer event raises.

argTupletuple, optional

Tuple of parameters passed as arguments to the callback function.

singleShotbool, optional

Defines if this timer only fires one time after its start (True) or continuously (False, default).

namestr, optional

Is the optional name of this timer. This name is displayed in the timer manager dialog (instead of the timer ID, if no name is given.

startAfterInitbool, optional

If this optional boolean is set to False the timer will not start after initialization. The timer can be started later by using timer.start().

Examples

>>> import time 
... 
... def callbackFunc(startTime: float, a: int): 
...     print("%.2f sec elapsed: %i" % (time.time() - startTime, a)) 
... 
... myTimer = timer(1000, callbackFunc, argTuple = (time.time(), 25)) 

1.00 sec elapsed: 25 2.01 sec elapsed : 25 3.01 sec elapsed : 25 4.01 sec elapsed : 25

isActive() bool

Indicates if the timer is currently active.

Returns
bool

True if the timer is running, otherwise False.

setInterval(interval)

Sets the timer interval in ms.

This method sets the timeout interval in milliseconds. If the timer is started, the callback function is tried to be continously triggered whenever the interval expired.

Parameters
intervalint

Timeout interval in milliseconds.

Notes

If Python is currently busy, a timer event can also be triggered at a later time, if the same trigger event is not already in the execution queue.

start()

Starts the timer.

This method starts or restarts the timer with its timeout interval. If the timer is already running, it will be stopped and restarted.

See also

isActive, stop
stop()

Stops the timer.

This method stop the timer (if currently active).

See also

isActive, start