繁体   English   中英

__call__() 缺少 1 个必需的位置参数:App Engine 上的“发送”FastAPI

[英]__call__() missing 1 required positional argument: 'send' FastAPI on App Engine

尝试在 App Engine 上托管 API 时,不断出现以下错误。 该程序曾经在 Flask 上运行,但运行速度很慢。

错误:

"Traceback (most recent call last):
  File "/env/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 134, in handle
    self.handle_request(listener, req, client, addr)
  File "/env/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: __call__() missing 1 required positional argument: 'send'
"

Docker 文件:

FROM gcr.io/google_appengine/python

RUN apt-get update && apt-get install -y ffmpeg

# Create a virtualenv for dependencies. This isolates these packages from
# system-level packages.

RUN virtualenv /env -p python3.7

# Setting these environment variables are the same as running
# source /env/bin/activate.

ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt

# Add the application source code.

ADD . /app

CMD gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app

app.yaml

runtime: custom
env: flex
entrypoint: gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app
service: encoder

runtime_config:
  python_version: 3

handlers:

- url: /.*
  script: auto

正如达斯汀所说,我发现需要更改工人 class 。 试试下面的一个。

gunicorn -k uvicorn.workers.UvicornWorker main:app

github 问题上找到了这个

App Engine 需要您的main.py文件来声明与WSGI Application对应的app变量。

由于 FastAPI 是一个异步的 web 框架,它不兼容 WSGI(它是同步的)。

您最好的选择是使用像Cloud Run这样的服务,它允许您定义自己的运行时并使用与 FastAPI 兼容的异步 HTTP 服务器。

当我想将 FastAPI 应用程序部署到 Heroku 时,我遇到了同样的问题。 实际上,您不能将uvicorn (FastAPI 正在使用的 ASGI 框架)与使用 gunicorn 的gunicorn一起使用。

但是,通过向 gunicorn 添加一个uvicorn工作者, gunicorn可以工作了::

gunicorn api:app --bind 0.0.0.0:$PORT --worker-class uvicorn.workers.UvicornWorker

我通过将python main.py作为构建命令并将我的 uvicorn 命令在 main.py 中设置为uvicorn.run("main:app", host="0.0.0.0", port=8080)解决了这个问题

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM