gpt4 book ai didi

python - 如何在 python Flask api 应用程序启动时处理 FlaskInjector 错误?

转载 作者:行者123 更新时间:2023-12-01 00:56:15 25 4
gpt4 key购买 nike

我用 python 和堆栈建立了 API 的最基本结构

flask -连接-flaskInjector

根据下面所示的代码。当 API 运行并初始化 FlaskInjector 时,它会失败并显示错误消息:

injector.CallError: Call to RequestScope.init() failed: init() missing 1 required positional argument: 'injector' (injection stack: [])

我的系统是Ubuntu 18.04,conda环境下的包如下:

conda 列表的相关摘录:

# Name                    Version                   Build  Channel
...
connexion 2.2.0 pypi_0 pypi
...
elasticsearch 7.0.1 pypi_0 pypi
fastavro 0.21.23 pypi_0 pypi
flask 1.0.2 pypi_0 pypi
flask-cors 3.0.7 pypi_0 pypi
flask-injector 0.12.0 pypi_0 pypi
flask-opentracing 1.0.0 pypi_0 pypi
flask-restplus 0.9.2 pypi_0 pypi
flask-script 2.0.6 pypi_0 pypi
flask-sqlalchemy 2.3.2 pypi_0 pypi
...
injector 0.12.0 pypi_0 pypi
...
swagger-ui-bundle 0.0.3 pypi_0 pypi
...

该项目的组织如下:

./app.py:

from injector import Binder
from flask_injector import FlaskInjector
from connexion.resolver import RestyResolver
import connexion
from providers.provider import ItemsProvider


def configure(binder: Binder) -> Binder:
binder.bind(
ItemsProvider,
ItemsProvider([{'Name': 'Test 1'}])
)
return binder


if __name__ == '__main__':
app = connexion.App(__name__, specification_dir='./swagger/')
app.add_api('app.yaml', resolver=RestyResolver('api'))
FlaskInjector(app=app.app, modules=[configure])
app.run(port=9090, debug=True)

./api/items.py:

from flask_injector import inject
from providers.provider import ItemsProvider


@inject(data_provider=ItemsProvider)
def search(data_provider) -> list:
return data_provider.get()

./providers/provider.py:

class ItemsProvider(object):
def __init__(self, items: list = []):
self._items = items

def get(self, number_of_items: int = 5) -> list:
if not self._items:
return []

if number_of_items > len(self._items):
number_of_items = len(self._items)

return self._items[0:number_of_items]

./swagger/app.yaml:

swagger: "2.0"

info:
title: "My first API"
version: "1.0"

basePath: /api

paths:
/items/:
get:
responses:
'200':
description: 'Fetch a list of items'
schema:
type: array
items:
$ref: '#/definitions/Item'

definitions:
Item:
type: object
properties:
id:
type: integer
format: int64
name: { type: string }

使用 python app.py 运行应用程序时,api 服务器按预期启动:

 * Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:9090/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 498-670-595

但是,在浏览器中寻址服务器或项目端点时,会出现以下失败堆栈:

127.0.0.1 - - [21/May/2019 08:16:31] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/app.py", line 1811, in full_dispatch_request
rv = self.preprocess_request()
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask/app.py", line 2087, in preprocess_request
rv = func()
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/flask_injector.py", line 327, in reset_request_scope_before
injector_not_null.get(request_scope_class).prepare()
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/injector.py", line 738, in get
result = scope_instance.get(key, binding.provider).get(self)
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/injector.py", line 144, in get
return injector.create_object(self._cls)
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/injector.py", line 789, in create_object
(), additional_kwargs, e, self._stack,)
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/injector.py", line 73, in reraise
raise exception.with_traceback(tb)
File "/home/christian/.conda/envs/miccroservice/lib/python3.6/site-packages/injector.py", line 782, in create_object
init(instance, **additional_kwargs)
injector.CallError: Call to RequestScope.__init__() failed: __init__() missing 1 required positional argument: 'injector' (injection stack: [])
127.0.0.1 - - [21/May/2019 08:16:31] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [21/May/2019 08:16:31] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [21/May/2019 08:16:31] "GET /?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [21/May/2019 08:16:31] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

最佳答案

谢谢您的回答。与此同时,我找到了一个可行的解决方案。 @inject 装饰器至少在我当前的版本中不能与参数一起使用。如果我将函数更改为:

@inject
def search(dataprovider=ItemsProvider_()) -> list:
data = dataprovider.get(0)
return data

谢谢!

关于python - 如何在 python Flask api 应用程序启动时处理 FlaskInjector 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56232231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com