What's the difference between threading.Timer and threading.Thread?
When I use the Timer function, it starts target command in separate thread with delay.
When I use the Thread function, it starts target command with args in separate thread.
It's the same, I think, isn't it?
It's worth looking at the implementation.
Thus, yes, a Timer is just an instance of Thread that invokes your function after a given delay, using the Event mechanism for efficient scheduling and providing a
cancel()that reliably prevents the function from being called at all if the delay has not yet expired.As we see above,
Threadis the implementation that does the heavy lifting, andTimeris a helper that adds extra functionality (a delay at the front);Timer(0, func)wouldn't work ifThreaddidn't exist.A
Threadbeing an object gives more flexibility than being only able to override the function to be called, without having that function having access to instance variables -- look atTimeritself, and how it uses that capability to add and respectcancel(). You can't do that on top ofTimerwithout subclassing_Timer, which (as the name implies) is an implementation detail not promised to be stable in future releases.Thus,
Timer()has more moving parts and is less flexible thanThread()-- both of those being innate disadvantages in software engineering -- but is handy for a specific moderately-common use case. Why would one not provide both?