我正在开发一个将在用户计算机上运行的 Python 程序。该程序应在操作之间尽可能空闲(每天 3 个)。行动应该在早上、下午和晚上。
所以我想告诉程序它必须在每天的三个特定时间运行(实例化一个类并从中执行一些方法)。
我知道一种方法,但我认为这是一种矫枉过正。
while True:
time = time.now()
if time in [08:00 am, 12:30pm, 08:00 pm]:
#Do what you have to do
你能告诉我最好的编程方法吗?该程序不会在我的计算机上运行,因此我想将所有内容作为一个组件进行管理。
编辑:Martin 建议我使用 cron 作业。是否可以在 Python 代码中设置所有内容,这样我就不必在客户端 PC 上更改任何内容?
最佳答案
由于您使用的是 Windows,请考虑使用 SCHTASKS
命令:
Schedules commands and programs to run periodically or at a specific time. Adds and removes tasks from the schedule, starts and stops tasks on demand, and displays and changes scheduled tasks.
你的命令应该是这样的:
schtasks /create /tn <TaskName> /tr <TaskRun> /sc hourly [/mo {1 - 23}] [/st <HH:MM>] [/sd <StartDate>] [/ed <EndDate>]
/mo
为重复间隔
完整文档请引用: https://technet.microsoft.com/en-ca/library/cc725744.aspx
关于python - 如何告诉程序每天在准确的时间做某事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30979938/