Search code examples
pythonpython-3.xtimer

Python: how to set a time to trigger a function?


How can I set a timer to run a function? right now I manually run the function. But what if I want to tell Python to run it in like 1 hour? how can I achieve this task? Additionally, how about trying to run it at say 5pm today or tomorrow?

I tried the following but does not really work. What did I miss?

import datetime
from threading import timer
def hello_world():
    print("hello world")
delta_t = datetime.time(0,1,0)
Timer(delta_t, hello_world)

Solution

  • import threading
    
    def task():
        print("hello world ")
    
    timer = threading.Timer(5.0, task)
    timer.start()