在 python3 中使用 Flask-APScheduler 后台定时任务时,发现定时任务会启动执行2次引发一些问题。
class Config:
td = timedelta(minutes=1)
run_date = datetime.now() + td
JOBS = [
{
'id': 'job1',
'func': 'app:job1',
'args': (4, 5),
'trigger': 'date',
'run_date': run_date.strftime('%Y-%m-%d %H:%M:%S')
}
]
SCHEDULER_API_ENABLED = True
原以为是 Flask-APScheduler 模块的配置问题,后发现并不是,只因为开启了 debug 模式,将 debug 模式禁掉就运行正常了。
在 stackoverflow 上有一个详细的介绍
https://stackoverflow.com/questions/25504149/why-does-running-the-flask-dev-server-run-itself-twice
资料:
http://flask.pocoo.org/docs/1.0/api/#flask.Flask.run
http://werkzeug.pocoo.org/docs/0.14/serving/#werkzeug.serving.run_simple