在 Flask-APScheduler 作业中查询模型引发应用程序上下文 RuntimeError
Querying model in Flask-APScheduler job raises app context RuntimeError
我想 运行 使用 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()
from models import User
def my_job():
user = User.query.first()
print(user)
查询过程中出现错误,无法打印。数据库在应用程序的其余部分中用于其他查询。
我在设置扩展程序时尝试添加 with app.app_context():
,但没有成功。
with app.app_context()
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
完整的追溯是:
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, in myjob
user = User.query.all()
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 454, in __get__
return type.query_class(mapper, session=self.sa.session())
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 71, in __call__
return self.registry()
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 878, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 704, in create_session
return SignallingSession(self, **options)
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 149, in __init__
self.app = db.get_app()
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 845, in get_app
raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context
Flask-SQLAlchemy 需要一个活动的应用上下文来执行查询。虽然 Flask-APScheduler 确实将 APScheduler 与 Flask 集成,但它不会在 运行 每个作业时推送应用程序上下文。
您需要在作业中推送应用上下文。设置扩展程序时推送应用程序上下文不会执行任何操作。
def my_job():
with app.app_context():
...
您可能需要所有作业的应用上下文。您可以子类化扩展并覆盖 run_job
.
from flask_apscheduler import APScheduler as _BaseAPScheduler
class APScheduler(_BaseAPScheduler):
def run_job(self, id, jobstore=None):
with self.app.app_context():
super().run_job(id=id, jobstore=jobstore)
# super(APScheduler, self) in Python 2
我通过将应用程序添加到应用程序工厂中的 SQLAlchemy 实例来修复此问题:
def create_app():
new_app = Flask(__name__)
new_app.config.from_object('config')
new_app.secret_key = os.urandom(12)
db.init_app(new_app)
db.app = new_app
return new_app
我曾尝试使用应用程序上下文,但从未奏效。
我想 运行 使用 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()
from models import User
def my_job():
user = User.query.first()
print(user)
查询过程中出现错误,无法打印。数据库在应用程序的其余部分中用于其他查询。
我在设置扩展程序时尝试添加 with app.app_context():
,但没有成功。
with app.app_context()
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
完整的追溯是:
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, in myjob
user = User.query.all()
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 454, in __get__
return type.query_class(mapper, session=self.sa.session())
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 71, in __call__
return self.registry()
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 878, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 704, in create_session
return SignallingSession(self, **options)
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 149, in __init__
self.app = db.get_app()
File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 845, in get_app
raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context
Flask-SQLAlchemy 需要一个活动的应用上下文来执行查询。虽然 Flask-APScheduler 确实将 APScheduler 与 Flask 集成,但它不会在 运行 每个作业时推送应用程序上下文。
您需要在作业中推送应用上下文。设置扩展程序时推送应用程序上下文不会执行任何操作。
def my_job():
with app.app_context():
...
您可能需要所有作业的应用上下文。您可以子类化扩展并覆盖 run_job
.
from flask_apscheduler import APScheduler as _BaseAPScheduler
class APScheduler(_BaseAPScheduler):
def run_job(self, id, jobstore=None):
with self.app.app_context():
super().run_job(id=id, jobstore=jobstore)
# super(APScheduler, self) in Python 2
我通过将应用程序添加到应用程序工厂中的 SQLAlchemy 实例来修复此问题:
def create_app():
new_app = Flask(__name__)
new_app.config.from_object('config')
new_app.secret_key = os.urandom(12)
db.init_app(new_app)
db.app = new_app
return new_app
我曾尝试使用应用程序上下文,但从未奏效。