Describing Timer(interval, func, args)
- A
Timerobject is used to execute a function at a later time - It creates a timer object that runs the function
funcafterintervalseconds - The
argsprovide the arguments and keyword arguments passed to thefunc - The timer does not start until the
startmethod is called
Describing t.start()
- The
startfunction is an instance method ofTimer - It starts the timer
- The function
funcsupplied toTimerwill be executed after the specified timer interval
Describing t.cancel()
- The
cancelfunction 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