我想用Flask-APScheduler运行一个查询Flask-SQLAlchemy模型的工作.当工作开始时,我明白了RuntimeError: application not registered on db instance and no application bound to current context
.如何运行查询数据库的作业.
from flask_apscheduler import APScheduler
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
Run Code Online (Sandbox Code Playgroud)
from models import User
def my_job():
user = User.query.first()
print(user)
Run Code Online (Sandbox Code Playgroud)
查询期间发生错误,然后才能打印.数据库正在其他应用程序中工作以进行其他查询.
我试图with app.app_context():
在设置扩展时添加,但这不起作用.
with app.app_context()
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
Run Code Online (Sandbox Code Playgroud)
完整的追溯是:
ERROR:apscheduler.executors.default:Job "run_in (trigger: interval[0:00:10], next run at: 2016-10-18 23:00:53 CEST)" raised an exception
Traceback (most recent call last):
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/apscheduler/executors/base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/Users/user/Documents/myfolder/myfolder/myfile.py", line 19, …
Run Code Online (Sandbox Code Playgroud)