• <small id='xWJQh'></small><noframes id='xWJQh'>

      <bdo id='xWJQh'></bdo><ul id='xWJQh'></ul>

    <i id='xWJQh'><tr id='xWJQh'><dt id='xWJQh'><q id='xWJQh'><span id='xWJQh'><b id='xWJQh'><form id='xWJQh'><ins id='xWJQh'></ins><ul id='xWJQh'></ul><sub id='xWJQh'></sub></form><legend id='xWJQh'></legend><bdo id='xWJQh'><pre id='xWJQh'><center id='xWJQh'></center></pre></bdo></b><th id='xWJQh'></th></span></q></dt></tr></i><div id='xWJQh'><tfoot id='xWJQh'></tfoot><dl id='xWJQh'><fieldset id='xWJQh'></fieldset></dl></div>
    1. <tfoot id='xWJQh'></tfoot>
      <legend id='xWJQh'><style id='xWJQh'><dir id='xWJQh'><q id='xWJQh'></q></dir></style></legend>

        在Flask-APScheduler作业中查询模型会引发应用程序上下文运行错误

        时间:2024-04-20

          <tbody id='ASGif'></tbody>
          <bdo id='ASGif'></bdo><ul id='ASGif'></ul>
          • <i id='ASGif'><tr id='ASGif'><dt id='ASGif'><q id='ASGif'><span id='ASGif'><b id='ASGif'><form id='ASGif'><ins id='ASGif'></ins><ul id='ASGif'></ul><sub id='ASGif'></sub></form><legend id='ASGif'></legend><bdo id='ASGif'><pre id='ASGif'><center id='ASGif'></center></pre></bdo></b><th id='ASGif'></th></span></q></dt></tr></i><div id='ASGif'><tfoot id='ASGif'></tfoot><dl id='ASGif'><fieldset id='ASGif'></fieldset></dl></div>

              1. <tfoot id='ASGif'></tfoot>

                <legend id='ASGif'><style id='ASGif'><dir id='ASGif'><q id='ASGif'></q></dir></style></legend>

                • <small id='ASGif'></small><noframes id='ASGif'>

                  本文介绍了在Flask-APScheduler作业中查询模型会引发应用程序上下文运行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我希望使用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
                  

                  推荐答案

                  我通过将应用程序添加到应用程序工厂中的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作业中查询模型会引发应用程序上下文运行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:调试:获取从中调用函数的文件名和行号? 下一篇:如何使用Selenium Python在#dow-root(Open)中提取信息?

                  相关文章

                  <i id='cObwx'><tr id='cObwx'><dt id='cObwx'><q id='cObwx'><span id='cObwx'><b id='cObwx'><form id='cObwx'><ins id='cObwx'></ins><ul id='cObwx'></ul><sub id='cObwx'></sub></form><legend id='cObwx'></legend><bdo id='cObwx'><pre id='cObwx'><center id='cObwx'></center></pre></bdo></b><th id='cObwx'></th></span></q></dt></tr></i><div id='cObwx'><tfoot id='cObwx'></tfoot><dl id='cObwx'><fieldset id='cObwx'></fieldset></dl></div>
                  <legend id='cObwx'><style id='cObwx'><dir id='cObwx'><q id='cObwx'></q></dir></style></legend>

                  <small id='cObwx'></small><noframes id='cObwx'>

                      • <bdo id='cObwx'></bdo><ul id='cObwx'></ul>
                      <tfoot id='cObwx'></tfoot>