python gunicorn -w 4 -b 0.0.0.0:8080 main:app 报错 TypeError: __call__() missing 1 required positional argument: 'send'

使用 gunicorn -w 4 -b 0.0.0.0:8080 main:app 运行时,报错 TypeError: __call__() missing 1 required positional argument: 'send'

from fastapi import FastAPI
app = FastAPI()
@app.post("/tet")
async def root():
    return {"message": "Hello World"}

运行命令:

gunicorn -k gevent --bind "0.0.0.0:8080" --log-level debug main:app
gunicorn -k tornado --bind "0.0.0.0:8080" --log-level debug main:app

报错信息

email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
[2020-06-08 16:05:42 +0800] [12796] [DEBUG] 1 workers
[2020-06-08 16:05:49 +0800] [12799] [DEBUG] GET /tet
[2020-06-08 16:05:49 +0800] [12799] [ERROR] Error handling request /tet
Traceback (most recent call last):
  File "/home/ap/nlp/Anaconda3/lib/python3.6/site-packages/gunicorn/workers/base_async.py", line 56, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/home/ap/nlp/Anaconda3/lib/python3.6/site-packages/gunicorn/workers/ggevent.py", line 160, in handle_request
    addr)
  File "/home/ap/nlp/Anaconda3/lib/python3.6/site-packages/gunicorn/workers/base_async.py", line 107, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: __call__() missing 1 required positional argument: 'send'

原因:

FastAPI only runs in an ASGI server. gunicorn only ships with implementations of the PEP 3333 WSGI standard. The two are not compatible.
FastAPI 仅在 ASGI 服务器中运行。gunicorn 仅附带 PEP 3333 WSGI 标准的实现。两者不兼容。

需要指定 uvicorn.workers.UvicornH11Worker

gunicorn -k uvicorn.workers.UvicornWorker --bind "0.0.0.0:8080" --log-level debug main:app

posted @ 2022-06-13 18:22  darling331  阅读(1142)  评论(0编辑  收藏  举报