我们的思路:是创建flask app对象的时候直接把后台任务调度器创建好
在工厂函数(创建flask app对象的函数)
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ThreadPoolExecutor
# 创建APScheduler定时任务对象
# 指定执行器的线程方式
executers={
'default':ThreadPoolExecutor(10)
}
app.scheduler=BackgroundScheduler(executers=executers)
from schedule.statistic import fix_statistics #导入调用的方法
# 添加“静态的”定时任务(与启动程序无关)
# app.scheduler.add_job(fix_statistics, 'corn', hour=3)
app.scheduler.add_job(fix_statistics,'date') # 测试使用
# 启动定时任务调度器
app.scheduler.start()
问题:fix_statistic这个定时任务在执行的时候用到了app_context上下文,现在采用 BackgroundScheduler的方式是否任然需要上下文呢?答案是需要的:原因在于schedule调度器执行的时机并