Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat a function every N minutes? [duplicate]

In my python script I want to repeat a function every N minutes, and, of course, the main thread has to keep working as well. In the main thread I have this:

# something
# ......
while True:
  # something else
  sleep(1)

So how can I create a function (I guess, in another thread) which executes every N minutes? Should I use a timer, or Even, or just a Thread? I'm a bit confused.

like image 287
Steven Avatar asked Feb 15 '16 13:02

Steven


1 Answers

use a thread

import threading

def hello_world():
    threading.Timer(60.0, hello_world).start() # called every minute
    print("Hello, World!")

hello_world()
like image 179
danidee Avatar answered Oct 24 '22 04:10

danidee