Connexion和Flask:在请求处理程序中访问db

时间:2019-05-02 16:07:06

标签: flask connexion

我想在java -XX:+UseContainerSupport $JAVA_OPTIONS -jar ../sample-service.jar --spring.config.additional-location=file:/app/sample-service-config.yml 应用程序的终结点处理程序中访问db对象。

当前,我将其附加到app.py中的connexion

current_app

,然后在handlers.py

中访问它
import connexion
from flask import current_app
from flask_sqlalchemy import SQLAlchemy


connexion_app = connexion.App(__name__, specification_dir="./")
connexion_app.add_api("swagger.yaml")
db = SQLAlchemy(connexion_app.app)

with connexion_app.app.app_context():
    current_app.db = db


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

这是正确的方法,还是我应该更喜欢Flask的from flask import current_app def cars(): data = current_app.db.query(...) return data 上下文变量而不是g?它们都是上下文变量,但是何时使用却有些混乱。

由于以下原因,无法在handlers.py中执行current_app

from app import db

这可能是由于循环依赖或由 ... connexion_app.add_api("swagger.yaml") File "/home/user/.virtualenvs/my_venv/lib/python3.6/site-packages/connexion/apps/flask_app.py", line 54, in add_api api = super(FlaskApp, self).add_api(specification, **kwargs) File "/home/user/.virtualenvs/my_venv/lib/python3.6/site-packages/connexion/apps/abstract.py", line 155, in add_api options=api_options.as_dict()) File "/home/user/.virtualenvs/my_venv/lib/python3.6/site-packages/connexion/apis/abstract.py", line 108, in __init__ self.add_paths() File "/home/user/.virtualenvs/my_venv/lib/python3.6/site-packages/connexion/apis/abstract.py", line 213, in add_paths self._handle_add_operation_error(path, method, err.exc_info) File "/home/user/.virtualenvs/my_venv/lib/python3.6/site-packages/connexion/apis/abstract.py", line 227, in _handle_add_operation_error six.reraise(*exc_info) File "/home/user/.virtualenvs/my_venv/lib/python3.6/site-packages/six.py", line 693, in reraise raise value File "/home/user/.virtualenvs/my_venv/lib/python3.6/site-packages/connexion/resolver.py", line 61, in resolve_function_from_operation_id return self.function_resolver(operation_id) File "/home/user/.virtualenvs/my_venv/lib/python3.6/site-packages/connexion/utils.py", line 96, in get_function_from_name function = deep_getattr(module, attr_path) File "/home/user/.virtualenvs/my_venv/lib/python3.6/site-packages/connexion/utils.py", line 55, in deep_getattr return functools.reduce(getattr, attr.split('.'), obj) AttributeError: module 'api.handlers' has no attribute 'cars' 和相关机器触发的某种情况引起的。


这是我的connexion_app.add_api("swagger.yaml"),仅供参考。

swagger.yml

0 个答案:

没有答案