APSheduler 中有 run_pending() 吗?

问题描述

我想在每天 00:00 运行特定的代码,所以我正在使用 APSheduler 但我不知道如何运行错过的作业;在调度程序中,它是一个运行挂起作业的 run_pending(),我不知道 APSheduler 中是否有一个,或者我不知道如何使用 APSheduler。

这是我的代码

#Libraries
from apscheduler.schedulers.background import BackgroundScheduler #Import background scheduler
import PREFS
                              
beginDate = datetime.date.today()
beginDate = beginDate.strftime("%Y/%m/%d")
prefs = {"kanjiNum": 0,"studyToday": 0,"beginDate": beginDate,"lostKanji": 0}
MainPrefs = PREFS.PREFS(prefs = prefs,filename = "DailyDoSEOfKanjis_Prefs") #Defining prefs

####
scheduler = BackgroundScheduler()
scheduler.start() #initializing scheduler
####

def DailyCheck():
    if int(MainPrefs.ReadPrefs()["studyToday"]) == "1":
        print("You did study")
        MainPrefs.WritePrefs("studyToday",0)
    elif int(MainPrefs.ReadPrefs()["studyToday"]) == "0":
        print("You didn't study")
        MainPrefs.WritePrefs("lostKanji",int(MainPrefs.ReadPrefs()["lostKanji"]) + 1)

    MainPrefs.WritePrefs("kanjiNum",BeginKanji())

####
scheduler.add_job(lambda: DailyCheck(),'cron',second = 0,minute = 0,hour = 0,id = 'check') #Adding daily jobs
scheduler.add_job(lambda: print("yea"),minute = 51,hour = 8,id = 'yea') #Adding daily jobs
####

解决方法

最后我放弃了 apscheduler 并使用 datetime 制定了我自己的逻辑。 使用存储首选项库我保存比较日期总是你关闭程序,当你打开它时在今天和比较日期之间进行迭代,在循环内执行我的每日检查并在程序更改结束时与今天的比较日期。

import PREFS # To save preferences (https://github.com/Patitotective/PREFS)
import datetime

Mprefs = lambda: {"compareDate": datetime.date.today().strftime("%Y/%m/%d")}
MainPrefs = PREFS.PREFS(prefs = Mprefs,filename = "Prefs/MainPrefs")

def DailyCheck():
    print("You didn't use me for one long day") #Here your code that you want to execute daily

def RunDailyCheck(): 
    today = datetime.date.today().strftime("%Y/%m/%d")
    for i in range(DaysBetween(today,MainPrefs.ReadPrefs()["compareDate"])): #For loop between the difference in days
        DailyCheck()

    MainPrefs.WritePrefs("compareDate",datetime.date.today().strftime("%Y/%m/%d")) #Set the compare date to today because it already executed the daily check

# Calculate the days between two dates
def DaysBetween(d1,d2):
    d1 = datetime.datetime.strptime(d1,"%Y/%m/%d")
    d2 = datetime.datetime.strptime(d2,"%Y/%m/%d")
    return abs((d2 - d1).days)


RunDailyCheck() #Run the pending checks,if you open the program every day never will execute DailyCheck()
#Your program
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#End of your program

MainPrefs.WritePrefs("compareDate",datetime.date.today().strftime("%Y/%m/%d")) #Set the compare date to today when you close the program

如果您不了解 PREFS 库,请查看它的文档:https://github.com/Patitotective/PREFS/wiki

,

您可以尝试使用 simple-scheduler,因为它很简单!

from time import ctime,time
from simple_scheduler.event import event_scheduler

def f():
    print(ctime(time()))

event_scheduler.timezones()
TZ = "Asia/Kolkata"
WHEN = ["mon|09:**","*|10:45"] #[mon/tue/wed/thu/fri/sat/sun] or "*" for all days

event_scheduler.add_job(target = f,when = WHEN,tz = TZ,job_name = "f")
event_scheduler.job_summary()
event_scheduler.run()
event_scheduler.job_summary()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...