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