duckman
duckman

Reputation: 747

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)

Upvotes: 2

Views: 1328

Answers (1)

GHOST5454
GHOST5454

Reputation: 69

import threading

def task():
    print("hello world ")

timer = threading.Timer(5.0, task)
timer.start()

Upvotes: 2

Related Questions