可配置的计时器

时间:2015-11-20 00:37:04

标签: timer

我正在尝试构建一个可通过配置文件(可能是XML)配置的安全系统。其中一个可配置选项是系统应录制的白天/黑夜。一周中的每一天都会有一个或更多的条目。我试图在python中实现这个,这不是我熟悉的语言。我的主要问题是如何干净利落地实施。基本上程序看起来像这样:

def check_if_on():
   """
   check if the system should be on based on current time and config file
   returns True iff system should be on, False otherwise
   """
   ...

# main loop
while True:
    # do something
    # do something else
    if check_if_on():
       # do something
    else:
       # do something else or nothing
    time.sleep(10)

配置文件看起来像:

<on-times>
  <on day="1" time="1900"/>
  <off day="2" time="0700"/>
  <on day="2" time="1800"/>
  <off day="3" time="0900"/>
</on-times>

比我经验丰富的朋友说将开/关时间作为队列中的定时事件来实现,但我不确定这是不是一个好主意,甚至不知道怎么做

1 个答案:

答案 0 :(得分:0)

如果不需要超精确计时,您可以每分钟运行一次作为cron作业来节省一些CPU周期。如果时间超过配置的时间,请执行某些操作,否则不执行任何操作。