我如何安排作业在每个月的最后一天始终通过APSCHEDULER运行

问题描述 投票:1回答:1

我有一个flask应用程序,该应用程序每分钟运行一次(向端点发送请求并存储一些数据)。但是现在我想做一份始终在每个月的最后一天运行的工作。

我已经存储了每个月的最后一天

from datetime import datetime
import calendar

now = datetime.now()
year = now.year
month = now.month
lastDay = calendar.monthrange(year, month)[1]

我做了一份工作,但是我不确定是每月还是一次?

def graphs():
    locale.setlocale(locale.LC_TIME, 'pt_PT.UTF-8')
    getMonth()

scheduler = BackgroundScheduler()
scheduler.add_job(func=graphs, trigger='cron', year=year, month=month, day=lastDay)
scheduler.start()
python apscheduler
1个回答
0
投票

[如果您希望它每个月,每年工作,您需要写:

from datetime import datetime
import calendar

def graphs():
    locale.setlocale(locale.LC_TIME, 'pt_PT.UTF-8')
    getMonth()

scheduler = BackgroundScheduler()
scheduler.add_job(func=graphs, trigger='cron', year='*', month='*', day='last')
scheduler.start()

与调度程序一起使用时,使用*last这样的表达式在每个值上触发或在一个月的最后一天触发非常有用。

有关更多详细信息,您可以检查此documentation

© www.soinside.com 2019 - 2024. All rights reserved.