all Python 3threadingterm
threading.Timer
threads
A thread that calls a function once after a given number of seconds; while it is still waiting, the call can be cancelled with cancel().
Syntax
t = threading.Timer(interval, function, args=None, kwargs=None)
t.start() / t.cancel()
Timer is a Thread subclass: nothing happens without start(), and the function runs in a separate thread, not the main one. It fires once — to repeat, create a new timer inside the function or loop over Event.wait(interval). cancel() only helps while the timer is still waiting: a function that has already started runs to the end. The interval counts from start(), not from construction, and a non-daemon timer keeps the program alive until it fires or is cancelled.
Examples
import threading
fired = []
t = threading.Timer(0.05, fired.append, args=['tick'])
t.start()
t.join()
print(fired, isinstance(t, threading.Thread)) # → ['tick'] True
import threading
fired = []
t = threading.Timer(0.5, fired.append, args=['tick'])
t.start()
t.cancel() # таймер ещё ждёт — отмена успевает
t.join()
print(fired, t.is_alive()) # → [] False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy