使用 UvicornWorker 时,Gunicorn 不考虑超时

Big*_*her 4 python gunicorn fastapi uvicorn

我正在设置超时检查,因此我进行了端点:

@app.get("/tc", status_code=200)
def timeout_check():
    time.sleep(500)
    return "NOT OK"
Run Code Online (Sandbox Code Playgroud)

我正在使用 docker 映像tiangolo/uvicorn-gunicorn-fastapi:python3.7 和命令来运行服务器:

CMD ["gunicorn","--log-level","debug","--keep-alive","15", "--reload", "-b", "0.0.0.0:8080", "--timeout", "15", "--worker-class=uvicorn.workers.UvicornH11Worker", "--workers=10", "myapp.main:app"]
Run Code Online (Sandbox Code Playgroud)

我预计端点会在 15 秒后失败,但事实并非如此。似乎没有遵守超时。有什么解决办法吗?

Big*_*her 6

异步工作线程的行为与同步工作线程不同:

  • 在同步工作线程中,工作线程将被阻止完成请求,因此如果请求花费的时间超过超时时间,工作线程将被终止,请求也将被终止。
  • 在异步工作线程中,即使请求需要很长时间,工作线程也不会被阻止并保持响应以满足其他请求。即,在这种情况下,工作超时和请求超时是不同的事情。

uvicorn 目前没有请求超时参数。

有关更多详细信息:https://github.com/benoitc/gunicorn/issues/1493