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

Jos*_*llo 29 python google-app-engine gunicorn google-cloud-platform fastapi

尝试在 App Engine 上托管 API 时,不断出现以下错误。该程序曾经在 Flask 上运行,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'
"
Run Code Online (Sandbox Code Playgroud)

泊坞窗文件:

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
Run Code Online (Sandbox Code Playgroud)

应用程序.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
Run Code Online (Sandbox Code Playgroud)

小智 38

正如达斯汀所说,我发现工人阶级需要改变。尝试下面的一个。

gunicorn -k uvicorn.workers.UvicornWorker main:app
Run Code Online (Sandbox Code Playgroud)

在github 问题上找到了这个

  • 这其实是最直接的答案。谢谢你的链接。 (3认同)

Dus*_*ram 7

App Engine 要求您的main.py文件声明一个appWSGI Application相对应的变量。

由于 FastAPI 是一个异步 Web 框架,因此它与 WSGI(同步)不兼容。

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


Ant*_*jnc 5

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

但是,通过添加一个uvicorn工作人员,gunicorn它就可以工作了!:

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