python - 我如何使用 APSCHEDULER 安排作业始终在该月的最后一天运行

标签 python apscheduler

我有一个 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()

最佳答案

如果你希望它每个月、每年都能工作,你需要写:

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

关于python - 我如何使用 APSCHEDULER 安排作业始终在该月的最后一天运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58079157/

相关文章:

python - 计算循环的结果?

Python 无法在我的桌面上找到文件

python apscheduler不一致

apscheduler - Advance python scheduler 'tuple' 对象在启动调度程序时没有属性 'public'

python - 使用APScheduler时如何处理异常?

Python 多个请求

python - 如何运行需要 app_context 的 apscheduler 作业

python - 您如何对 PNG 图像进行 base-64 编码以在 CSS 文件中的 data-uri 中使用?

python - py.test 将一个测试的结果传递给另一个

python - Windows文件夹结构中的 'access'和 'modified'次有什么区别?