python - 如何告诉程序每天在准确的时间做某事?

标签 python windows time runtime

我正在开发一个将在用户计算机上运行的 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/

相关文章:

php - PHP 中的日期分组

python importerror 没有名为 zope.interface 的模块扭曲

python - psycopg2如何处理TypeError : not all arguments converted during string formatting

c - 在 C 中加载 8bpp 灰度 BMP

go - 中午在Golang跑代码

ORACLE 不保存日期时间。为什么?

python - 无法在 Django、Virtualenv 中创建 super 用户

python - 如何键入提示具有封闭类类型的方法?

windows - 我可以将 Linux DMA 驱动程序移植到 Windows 吗?

javascript - RabbitMQ 管理插件窗口呈现为空白页