I noticed the following behavior in the following code (using threading.Timer class):
import threading
def ontimer():
print threading.current_thread()
def main():
timer = threading.Timer(2, ontimer)
timer.start()
print threading.current_thread()
timer.cancel()
if timer.isAlive():
print "Timer is still alive"
if timer.finished:
print "Timer is finished"
if __name__ == "__main__":
main()
The output of the code is:
<_MainThread(MainThread, started 5836)>
Timer is still alive
Timer is finished
As We notice from the output, that the timer object is still alive and finished in the same time.
In fact, I would like to call a similar function hundreds of times, and I wonder whether those "living" timers may affect the performance.
I would like to stop or cancel the timer object in a proper way. Am I doing it right?
Thank you
timer.finished
withtimer.finished.is_set()
. The propertytimer.finished
is not a boolean, it is an_Event
object, so checking forif timer.finished
is misleading (since that will always evaluate to True) – Benitez