【发布时间】:2023-03-23 20:20:02
【问题描述】:
如何在 Python 中在给定时间运行函数?
例如:
run_it_at(func, '2012-07-17 15:50:00')
它将在2012-07-17 15:50:00运行函数func。
我尝试了sched.scheduler,但它没有启动我的功能。
import time as time_module
scheduler = sched.scheduler(time_module.time, time_module.sleep)
t = time_module.strptime('2012-07-17 15:50:00', '%Y-%m-%d %H:%M:%S')
t = time_module.mktime(t)
scheduler_e = scheduler.enterabs(t, 1, self.update, ())
我能做什么?
【问题讨论】:
-
什么操作系统?您可能需要使用 python 外部的程序运行它,例如 unix 上的 cron。
-
为什么不呢?您为
enter设置了什么延迟(假设您尝试过)? -
你是如何尝试使用
sched.scheduler的? -
我用了
sched.scheduler,因为我用谷歌搜索了它:) -
你打电话给
scheduler.run()了吗?