首页 > 解决方案 > Python:如何设置触发函数的时间?

问题描述

如何设置计时器来运行功能?现在我手动运行该功能。但是如果我想告诉 Python 在 1 小时内运行它呢?我怎样才能完成这项任务?此外,尝试在今天或明天下午 5 点运行它怎么样?

我尝试了以下但并没有真正起作用。我错过了什么?

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

标签: pythonpython-3.xtimer

解决方案


import threading

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

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

推荐阅读