Flask 1.1.x RuntimeError: Working outside of request context
在fkask项目中要用到定时任务,选用了APScheduler,但是在定时任务中要用到上下文,于是按照APScheduler官方示例中使用with scheduler.app.app_context()方法,如下,结果发现没什么用,还是会报错
def blah():
with scheduler.app.app_context():
# do stuff
无奈,各种找解决办法,之后终于在flask官方文档中找到手动推送上下文的方法with app.test_request_context(),如下,这里附上链接,
ef generate_report(year):
format = request.args.get('format')
...
with app.test_request_context(
'/make_report/2017', data={'format': 'short'}):
generate_report()
之后,RuntimeError: Working outside of request context.问题解决,但随之,又出现另外一个问题,skipped: maximum number of running instances reached (1),需要设置一个参数,
max_instances默认值为1,它表示id相同的任务实例数。修改max_instances的值:
@scheduler.task('interval', id='do_job_1', seconds=10, max_instances=100)