In Python, if I do something like the answer in this thread: Executing periodic actions in Python
eg:
>>>import time, threading
>>> def foo():
... print(time.ctime())
... threading.Timer(10, foo).start()
...
>>> foo()
I understand that each 10
seconds I'm starting a new Timer
thread that will wait the time, then create a new timer, etc, and it will run indefinitely.
Obviously, there is nothing in the output of dir()
as I didn't assign it a name.
Is there any way to get a reference to this Timer
, for instance to .cancel()
it? Or is the only way to stop it to have kept a reference to it from the beginning?
I know this is very poor practice, but it demonstrates my more general question.