iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🍰

Running FastAPI with Gunicorn

に公開

What is FastAPI in the first place?

It is an API framework for Python.

Official website is here:
https://fastapi.tiangolo.com/

Quoting from the official site, it has the following features:

Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
Fast to code: Increase the speed to develop features by about 200% to 300%. *
Fewer bugs: Reduce about 40% of human (developer) induced errors. *
Intuitive: Great editor support. Completion everywhere. Less time debugging.
Easy: Designed to be easy to use and learn. Less time reading docs.
Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
Robust: Get production-ready code. With automatic interactive documentation.
Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.

My personal impression is that it is easier to use than Flask, has lower learning costs than Django, and above all, it automatically creates documentation, making it very user-friendly.

Main Topic - What do I want to do?

FastAPI can be started using uvicorn as follows:

pip install uvicorn
uvicorn main:app --reload

I was using this without much thought, but a team member pointed something out to me.
"The uvicorn manual has this entry:"

For production deployments we recommend using gunicorn with the uvicorn worker class.

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

https://www.uvicorn.org/#running-with-gunicorn

So, I tried to introduce gunicorn.

What I did

Installing gunicorn

poetry add gunicorn

I used the command above since I use poetry for Python package management, but for pip, I think the following works fine:

pip install gunicorn

Starting, then an error

gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker
ModuleNotFoundError: No module named 'uvloop'

An error occurred. It seems uvloop is missing.

Solution

Change the uvicorn entry in pyproject.toml as follows:

uvicorn = {extras = ["standard"], version = "^0.13.3"}

Note: Use the version you are currently using.
After the change, uninstall uvicorn once and reinstall it with poetry update.

If you are installing via pip:

pip install uvicorn[standard]

It seems that if you don't include [standard], only the minimum required modules are installed, resulting in an error because uvloop is missing.

Reference:
https://github.com/tiangolo/fastapi/issues/2181

Starting again

With the same startup command as before, it finally started successfully.

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

In the first place

The FastAPI documentation stated from the beginning to install it with the standard extra.

pip install uvicorn[standard]

https://fastapi.tiangolo.com/ja/#_3

Conclusion

I should read the official documentation properly.

Discussion