Describing Timer(interval, func, args)
- A
Timer
object is used to execute a function at a later time - It creates a timer object that runs the function
func
afterinterval
seconds - The
args
provide the arguments and keyword arguments passed to thefunc
- The timer does not start until the
start
method is called
Describing t.start()
- The
start
function is an instance method ofTimer
- It starts the timer
- The function
func
supplied toTimer
will be executed after the specified timer interval
Describing t.cancel()
- The
cancel
function is an instance method ofTimer
- It cancels the timer if the function has not started yet
Example of Timer
>>> from threading import Timer
>>> def sleepy():
... print('thread started')
... time.sleep(10)
... print('thread done')
>>> t = Timer(30.0, sleepy)
>>> t.start()
>>> print('waiting for 30 seconds')
'waiting for 30 seconds'
>>> print('still waiting')
'thread started'
'thread done'
References
Previous
Next