简体   繁体   English

在 python 中创建例程

[英]create a routine in python

So I need my program to run a task every given hour of a given day, I looked for a module that could easily do it and it exists, the schedule one, unfortunately we do not have it at my company's computer and I am not allowed to install anything, so I thought of something like this as a solution所以我需要我的程序在给定一天的每个给定小时运行一项任务,我寻找一个可以轻松完成它的模块并且它存在,时间表之一,不幸的是我们公司的计算机上没有它,我不允许安装任何东西,所以我想到了这样的解决方案

import datetime

now = datetime.datetime.now()
while True:
    if now.strftime("%d") == '09':
        if now.strftime("%H") == '18':
            do something

but it does not quite work as expected, if i Run the program at the schedulled time it runs normally, but if it's not, it does nothing when the time comes但它并没有像预期的那样工作,如果我在预定的时间运行程序,它会正常运行,但如果不是,那么到时候它什么也不做

what can I do?我能做些什么?

You've defined now outside the loop at the start of the program.now已经在程序开始时在循环之外定义了。 Once running, it never changes.一旦运行,它就永远不会改变。 Set now inside the loop, and consider using sleep to let this program not waste resources as much. now设置在循环内部,并考虑使用sleep让这个程序不会浪费太多资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM