Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How threading.timer works in python [duplicate]

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?

like image 845
Praniti Gupta Avatar asked Aug 11 '26 04:08

Praniti Gupta


1 Answers

Timer is a thread. Its created when you instantiate Timer(). That thread waits the given amount of time then calls the function. Since the function creates a new timer, yes, it is called every 5 seconds.

like image 58
tdelaney Avatar answered Aug 13 '26 08:08

tdelaney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!