I want to run a function every n seconds. After some research, I figured out this code:
import threading
def print_hello():
threading.Timer(5.0, print_hello).start()
print("hello")
print_hello()
Will a new thread be created every 5 sec when print_hello() is called?
Timeris a subclass ofThread.threading.Timerinstance is a single instance which represents a single thread. It can be made to run repetitive code, if the function it calls contains a loop. However, as explained in that documentation link,print_hellowill only be called once by thethreading.Timerobject. Multiple threads are created becauseprint_hellocreates a new instance each time.