如何在python计划模块中完成之前清除挂起的作业?

问题描述

实际上,我尝试了此解决方Schedule python clear jobs queue。它按我的预期工作。

但是,我正在制作一个简单的育儿程序,该程序从sqlite数据库文件获取时间。

this is my database

当时间到时,程序将显示一条通知。 5分钟后,将显示一个通知。第二次通知后30秒,程序将执行注销命令。

我的代码

def job():   
   toaster.show_toast(Title,MsgOne,icon_path=Icon) # 1st notification
   time.sleep(300) # Sleep for 5 mins
   toaster.show_toast(Title,MsgTwo,icon_path=Icon) # 2nd notification
   time.sleep(30) # Sleep for 30 sec
   os.system("shutdown -l") # logout 

schedule.every().day.at(TimeFajr).do(job) # Fajr
schedule.every().day.at(TimeDhuhr).do(job) # Dhuhr
schedule.every().day.at(TimeAsr).do(job) # Asr 
schedule.every().day.at(TimeMaghrib).do(job) # Maghrib 
schedule.every().day.at(TimeIshaa).do(job) # Isha'a 

while True:
   schedule.run_pending()
   time.sleep(1)

一切正常。即使将其转换为exe (使用自动py-to-exe模块) 。通过将其添加到Windows启动文件夹,使该程序在后台运行。

问题是,当我将计算机置于睡眠模式或锁定计算机时间到之前,并在时间过去后重新登录 ,该程序将运行 QUEUED JOB (排队作业)并注销系统,即使时间已过。我想要的是,排队的工作需要清除或不执行。我在上面的链接上尝试了该解决方案,但将其转换为exe后无法使用。没有这个,程序就可以正常工作。 (对不起,我的英语不好!)

仅供参考,我是python的新手,这是我的第一个项目。如果还有其他方法可以实现我正在做的事情,请指导我。

解决方法

更新:

我终于找到了一种方法。 我将整个 scheduler 模块替换为 APScheduler

我的代码中的一个片段:

from apscheduler.schedulers.blocking import BlockingScheduler



def job():   
    toaster.show_toast(Title,MsgOne,icon_path=Icon) # 1st notification
    time.sleep(180) # Sleep for 3 mins
    toaster.show_toast(Title,MsgTwo,icon_path=Icon) # 2nd notification
    time.sleep(30) # Sleep for 30 sec
    toaster.show_toast(Title,MsgThree,icon_path=Icon) # 3rd notification
    #ctypes.windll.PowrProf.SetSuspendState(0,1,0) # This will put the PC on sleep
    os.system("rundll32.exe user32.dll,LockWorkStation") # lock windows

scheduler.add_job(job,'cron',day_of_week='mon-sun',hour=fajr_H,minute=fajr_M,misfire_grace_time=1)
scheduler.add_job(job,hour=dhuhr_H,minute=dhuhr_M,hour=asr_H,minute=asr_M,hour=maghrib_H,minute=maghrib_M,hour=ishaa_H,minute=ishaa_M,misfire_grace_time=1)

scheduler.start()

这是我想要的。