如何在python中安排定时事件

如何在python中安排定时事件,python,scheduler,Python,Scheduler,我想在python中安排一个重复的定时事件,如下所示: “在时间X启动函数Y(在单独的线程中)并每小时重复一次” “X”是固定的时间戳 代码应该是跨平台的,所以我希望避免使用像“cron”这样的外部程序来实现这一点 代码摘录: import threading threading.Timer(10*60, mail.check_mail).start() #... SET UP TIMED EVENTS HERE while(1): print("plea

我想在python中安排一个重复的定时事件,如下所示: “在时间X启动函数Y(在单独的线程中)并每小时重复一次”

“X”是固定的时间戳

代码应该是跨平台的,所以我希望避免使用像“cron”这样的外部程序来实现这一点

代码摘录:

import threading threading.Timer(10*60, mail.check_mail).start() #... SET UP TIMED EVENTS HERE while(1): print("please enter command") try: command = raw_input() except: continue handle_command(command) 导入线程 threading.Timer(10*60,mail.check_-mail).start() #... 在此处设置定时事件 而(一): 打印(“请输入命令”) 尝试: 命令=原始输入() 除: 持续 handle_命令(command) 为您的计划创建一个,
rr
,然后在线程中使用如下循环:

for ts in rr:
    now = datetime.now()
    if ts < now:
        time.sleep((now - ts).total_seconds())
    # do stuff
对于rr中的ts:
now=datetime.now()
如果ts<现在:
time.sleep((now-ts).total_seconds())
#做事
或者一个更好的解决方案,可以考虑时钟变化:

ts = next(rr)
while True:
    now = datetime.now()
    if ts < now:
        time.sleep((now - ts).total_seconds() / 2)
        continue
    # do stuff
    ts = next(rr)
ts=next(rr)
尽管如此:
now=datetime.now()
如果ts<现在:
time.sleep((现在-ts).total_seconds()/2)
持续
#做事
ts=下一个(rr)

您需要提供有关应用程序的更多详细信息。它是否使用事件循环等。为什么不使用
cron
?在windows上,用户可以使用任务计划程序。