简体   繁体   English

Flask - 运行时错误:找不到应用程序。 在视图函数内工作或推送应用程序上下文

[英]Flask - RuntimeError: No application found. Either work inside a view function or push an application context

I'm now trying to make an application that processes HTTP requests with REST APIs.我现在正在尝试制作一个使用 REST API 处理 HTTP 请求的应用程序。 The application uses Flask for the web framework, and it uses Celery for the asynchronous tasks.该应用程序使用 Flask 作为 Web 框架,并使用 Celery 执行异步任务。

Here's the application structure.这是应用程序结构。

app.py
celery_app.py
controller/
    controller.py
task/
    task.py

(1) app.py (1) 应用程序.py

There are no lines for the Celery configuration. Celery 配置没有行。

from flask import Flask
...

app = Flask(__name__)
...


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

(2) celery.py (2) celery.py

from consts.consts import Consts
from kombu.utils.url import quote
from celery import Celery
from config.config import Config

config = Config()
db_uri = 'db+' + config.get_db_uri()

aws_access_key = quote(config.get_config(Consts.AWS, Consts.AWS_ACCESS_KEY))
aws_secret_key = quote(config.get_config(Consts.AWS, Consts.AWS_SECRET_KEY))
broker_url = "sqs://{aws_access_key}:{aws_secret_key}@".format(
    aws_access_key=aws_access_key, aws_secret_key=aws_secret_key
)

celery = Celery(
    broker=broker_url,
    backend=db_uri,
    include=['task']
)

(3) task/task.py (3)任务/task.py

I put all the tasks here.我把所有的任务都放在这里了。

from celery_app import celery
from flask import current_app as app
from model.model import db


@celery.task
def check_friend_status_from_db(user1, user2):
    status = db.engine.execute(
            "QUERY").fetchone()
    return status

Now, the controller/controller.py file imports and calls the tasks as follows.现在, controller/controller.py文件按如下方式导入和调用任务。

(4) controller/controller.py (4)控制器/控制器.py

from flask import Blueprint, request, json, render_template, jsonify
from mysql.connector import Error
from sqlalchemy import or_, and_
from consts.consts import StatusCode
from consts.consts import Consts
from model.model import db, FriendRequest
from outbound.request import Request
from util.logging import logger
from task.task import check_friend_status_from_db


controller_blueprint = Blueprint('controller_blueprint', __name__)
outbound_request = Request()


@controller_blueprint.route('/friend/status/<requested_from>/<requested_to>', methods=['GET'])
def check_friend_status(requested_from, requested_to):
    logger.info('Checking the friend status')
    try:
        status = check_friend_status_from_db.apply_async((requested_from, requested_to)).get()

        if status is None:
            response = {
                Consts.STATUS_CODE: StatusCode.OK,
                Consts.FRIEND_STATUS: StatusCode.NO_RELATION
            }
        else:
            response = {
                Consts.STATUS_CODE: StatusCode.OK,
                Consts.FRIEND_STATUS: status
            }
    except Error as e:
        logger.error("TypeError:", e)
        response = {
            Consts.STATUS_CODE: StatusCode.ERROR
        }
    json_response = jsonify(response)
    logger.info(json_response)
    return json_response

When I run the code, I get the error as I mentioned on the title.当我运行代码时,我收到标题中提到的错误。

RuntimeError: No application found. Either work inside a view function or push an application context

and it turns out to be this part under the try block in the controller where the error is coming from.结果是错误来自控制器中的 try 块下的这部分。

status = check_friend_status_from_db.apply_async((requested_from, requested_to)).get()

Any solutions, please?请问有什么解决办法吗?

It looks like you need to register your blueprint controller_blueprint .看起来您需要注册您的蓝图controller_blueprint Since this blueprint is not registered to your app , you are working outside the context of you application and hence the error.由于此蓝图未注册到您的app程序,因此您在应用程序的上下文之外工作,因此会出现错误。

You can do so in your app.py:你可以在你的 app.py 中这样做:

from controller.controller import controller_blueprint

app = Flask(__name__)
app.register_blueprint(controller_blueprint)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有蓝图的 Flask-sqlalchemy - 运行时错误:找不到应用程序。 在视图函数中工作或推送应用程序上下文 - Flask-sqlalchemy with Blueprints - RuntimeError: No application found. Either work inside a view function or push an application context RuntimeError:未找到应用程序。 在视图 function 内工作或推送应用程序上下文。 FLASK SQLAlchemy 错误 - RuntimeError: No application found. Either work inside a view function or push an application context. FLASK SQLAlchemy error 试图删除 flask 中的所有表,运行时错误:未找到应用程序。 在视图 function 内工作或推送应用程序上下文 - Trying to drop all tables in flask, RuntimeError: No application found. Either work inside a view function or push an application context Flask、concurrent.futures 和 SQLAlchemy - 找不到应用程序:在视图函数内工作或推送应用程序上下文 - Flask, concurrent.futures and SQLAlchemy - No application found: work inside a view function or push an application context 在 Flask 中推送应用程序上下文 - Push application context in Flask Flask - RuntimeError:在应用程序上下文之外工作 - Flask - RuntimeError: Working outside of application context Flask RuntimeError:在应用程序上下文之外工作 - Flask RuntimeError: working outside of application context RuntimeError:未找到应用程序。 无法加载问题标签 - RuntimeError: No application found. Can't load tags for questions 在 Flask function 上找不到应用程序 - No application found on Flask function Flask-pymongo RuntimeError:在应用程序上下文之外工作 - Flask-pymongo RuntimeError: Working outside of application context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM