tiangolo / uvicorn-gunicorn-fastapi-docker

Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python with performance auto-tuning.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uvicorn and Gunicorn, what is the difference?

rudi101101 opened this issue · comments

What is the different deployed dockerfile using Uvicorn and Tiangolo (Uvicorn Gunicorn)? and why my results show that deployed only using Uvicorn get a better result than Tiangolo? When I search to Tiangolo documentation, it says You can use Gunicorn to manage Uvicorn and run multiple of these concurrent processes. That way, you get the best of concurrency and parallelism., From this, I can assume that using this Gunicorn will get a better result?

This is my Testing using JMeter, I deployed my script to Google Cloud Run, and this is The result

Using Python (Uvicorn):

enter image description here

Using Tiangolo (Uvicorn Gunicorn):

enter image description here

This is my Dockerfile for Python (Uvicorn):

FROM python:3.8-slim-buster
RUN apt-get update --fix-missing
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y libgl1-mesa-dev python3-pip git
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip3 install -U setuptools
RUN pip3 install --upgrade pip
RUN pip3 install -r ./requirements.txt --use-feature=2020-resolver
COPY . /usr/src/app
CMD ["python3", "/usr/src/app/main.py"]

This is my Dockerfile for Tiangolo (Uvicorn Gunicorn):

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8-slim
RUN apt-get update && apt-get install wget gcc -y
RUN mkdir -p /app
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
RUN python -m pip install --upgrade pip
RUN pip install --no-cache-dir -r /app/requirements.txt
COPY . /app

You can see the error from Tiangolo, is it caused by Gunicorn?

@tiangolo I'm sorry for tagging you, can you give me some explanation from my case?

What is test 1 doing exactly? It's probably running into some kind of resource problem that's not visible if you run your app without Gunicorn as then your app will be limited by the requests per second FastAPI can serve with just one CPU core/thread.

For example, in case test 1 opens a database connection it's possible that the multiple workers spawned by Gunicorn will use more database connections than the database server is configured for, while Uvicorn alone might be CPU limited and thus not tax the database as much.

Same can happen when serving/opening files, e.g., exceeding ulimit -Hn on Linux, or pretty much any other resource (RAM, etc.).

There's a good summary of the problem here: encode/starlette#802 (comment)

Hey there! It's difficult to debug your specific use case without all the code and all the details of your benchmark/test. But anyway, there's a very high chance that you indeed don't need this image: https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker#-warning-you-probably-dont-need-this-docker-image

And you are most probably better off just building your own and running Uvicorn directly, assuming you are under Kubernetes or something similar.

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.