APScheduler中是否存在任何运行_挂起()?

2024-07-02 10:26:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想每天00:00运行特定的代码,所以我使用ApScheduler,但我不知道如何运行丢失的作业;在scheduler中,运行挂起作业的是run_pending(),我不知道ApScheduler中是否有挂起作业,或者我不知道如何使用ApScheduler

这是我的密码:

#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"), 'cron', second = 0, minute = 51, hour = 8,  id = 'yea') #Adding daily jobs
####

Tags: import作业prefsschedulerintbackgroundprintapscheduler
2条回答

最后,我放弃了ApsScheduler,用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()

相关问题 更多 >