Teach you step by step how to deploy a FastAPI service

This article uses the FastAPI framework. From this article you can learn:

  • How to configure and deploy FastAPIapplication to AIF
  • Why andWhen You Should Use FastAPI, Gunicorn and Uvicorn
  • How and why toset up a Gunicorn + Uvicorn combination as an ASGI server

Table of Contents

1. Get started directly

2. Preface

3. Introduction to technical framework

3.1 FastAPI

3.2 Uvicorn

3.3 Gunicorn

3.4 ASGI/WSGI

3.4.1 ASGI

3.4.2 WSGI

3.4.3 Comparison

4. Summarize


1. Get started

Simple and one-step operation!

from fastapi import FastAPIimport uvicorn
app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

if __name__ == '__main__':
    uvicorn.run(app='main:app', host="127.0.0.1", port=8000, reload=True, debug=True)

Start command

Using Process Manager ensures that you run multiple processes in a resilient manner so that you can perform server upgrades without dropping client requests.

A process manager will handle socket setup, start multiple server processes, monitor process activity, and listen for process restarts, shutdowns, and other signals.

1. Use uvicorn

uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4

Uvicorn provides a lightweight way to run multiple worker processes, such as `–workers 4`, but does not provide monitoring.

Uvicorn has advantages in using asynchronous coroutines in a single process, butif the interface method is synchronous, it will adopt multi-process mode, that is, the number of workers. In this case, uvicorn is not as good as gunicorn, and there will be some limitations in performance.

2. Use gunicorn

gunicorn main:app -b 0.0.0.0:8001 -w 4 -k uvicorn.workers.UvicornWorker
# `-w` represents the number of workers, `-k` represents the type of worker used, `-b` represents the bound address and port. 
  • Gunicorn is a mature, fully functional server that can set the process type , and Uvicorn can be used as the process type of Gunicorn.
  • Uvicorn contains Guicorn’s workers class internally, which allows you to run ASGI applications. These workers inherit all Uvicorn’s high-performance features.
  • Guicorn can perform process management. If a process dies, it will restart a new process to maintain the number of worker processes. You can dynamically increase or decrease the number of processes, smoothly restart worker processes, or upgrade Server without downtime.
  • Using this combination, Gunicorn will act as a process manager, listening on ports and IPs. Ittransfers the received data to the worker process running the Uvicorn class, which then transfers the data to FastAPI.
  • In a production environment, Guicorn is probably the easiest way to manage Uvicorn. For production environment deployment, we recommend using the worker classes of Guicorn and Uvicorn

At this point, you can deploy and start running a FastAPI service. If you just want to know how to operate, just see here. However, if you still want to know why uvicorn and gunicorn should be used, then please continue reading!

If you don’t understand the concepts of uvicorn and gunicorn, you can first think about this question based on the above content: uvicorn has the function of worker, why not just use uvicorn directly? gunicorn hangs uvicorn worker startup? Then take a look at the content with this question~

2. Foreword

FastAPI is one of the most popular Python libraries for developing API applications. It is known for its excellent performance and ease of use. If you use machine learning models in web applications, then it is probably your tool of choice.

NGINX, Gunicorn and Uvicorn are all proven technologies that are often used as reverse proxies and ASGI servers to deploy Python web applications. If you’re familiar with Django or Flask, you’ve probably heard of some of them before.

3. Introduction to technical framework

3.1 FastAPI

FastAPI is a modern, high-performance web framework for building APIs in Python and based on standard type hints.

It has the following main features:

  • Efficient operation: With Starlette and pydantic, FastAPI provides excellent performance similar to NodeJS and Go. FastAPI is much faster than Flask, it is actually one of the fastest web frameworks for Python. The only framework that is faster than FastAPI is Starlette (FastAPI is actually built on Starlette).
  • Rapid development: It can significantly increase the speed of development.
  • Reduced Errors: Reduces the possibility of human error.
  • Intuitive and easy to use: Supports powerful editor features, auto-completion and less time debugging.
  • Easy to learn: The design is simple and clear, so you can spend less time reading the documentation.
  • Reduce the amount of duplicate code: Minimize code duplication.
  • Robust and reliable: Provides production-ready code and automatic generation of interactive documentation.
  • Standardization-based: Follow open standards like API, OpenAPI, and JSON Schema.

The framework is designed to optimize the developer experience, allowing you to build best-practice, production-ready APIs with clean code.

3.2 Uvicorn

Unlike the Flask framework, FastAPI does not include any built-in development server. Therefore, we needUvicorn. It implements theASGIstandard and is lightning fast. ASGI stands for Asynchronous Server Gateway Interface.

Uvicorn is an ASGI web server implementation for Python.

3.3 Gunicorn

Gunicorn is an implementation of a WSGI server for Python applications.

Gunicorn is a WSGI compliant web server for Python applications that receives data sent from client to Webserver< requests and forward them to a Python application or Web framework (such as Flask or Django) to run the appropriate application code for the request.

3.4 ASGI/WSGI

ASGI (Asynchronous Server Gateway Interface) and WSGI (Web Server Gateway Interface) are both interface standards between Python web applications and web servers. They define how the web server communicates with web applications.

3.4.1 ASGI

  • ASGI is a more modern, asynchronous interface standard designed to support asynchronous web applications, such as Python frameworks based on async/await.
  • It allows web applications to handle large numbers of concurrent requests without blocking, thus improving performance and responsiveness.
  • Some common ASGI frameworks include FastAPI, Starlette, and Tornado.

ASGI Services:

  • Uvicorn is an ASGI server for running ASGI applications. It is a lightweight, fast and easy to deploy server widely used in FastAPI and other ASGI frameworks.

  • Hypercorn: Hypercorn is also an ASGI server, similar to Uvicorn, used to run ASGI applications. It has some additional features, such as support for UNIX sockets.

  • Daphne: Daphne is an ASGI server specifically designed for running Django applications. It allows Django applications to be deployed as ASGI applications.

3.4.2 WSGI

  • WSGI is an older interface standard for synchronous web applications.
  • It allows communication between Python web applications and web servers, but does not handle asynchronous operations efficiently.
  • Some common WSGI frameworks include Django, Flask, and Bottle.

WSGI service:

  • uWSGI: uWSGI is a WSGI server widely used to run WSGI applications. It supports multiple application containers and deployment modes and can be used with a variety of web servers.

  • Gunicorn: Gunicorn (Green Unicorn) is a WSGI server designed specifically for Django applications. It improves application performance by enabling concurrent processing of requests through multiple worker processes.

3.4.3 Comparison

The main differences are:

  • Synchronous vs. Asynchronous: WSGI is a synchronous interface, suitable for synchronous web applications. ASGI is an asynchronous interface, suitable for asynchronous web applications. ASGI allows applications to handle asynchronous operations such as long connections and WebSocket.

  • Performance: Due to the asynchronous nature of ASGI, it is more suitable for handling a large number of concurrent requests and high-performance requirements. WSGI is more suitable for traditional synchronous web applications.

  • Framework compatibility: ASGI is usually used in asynchronous frameworks such as FastAPI, Starlette, Tornado, etc., while WSGI is usually used in synchronous frameworks such as Django, Flask, Bottle, etc.

4. Summary

Now back to the original question, you can know that Uvicorn can indeed be run as an ASGI server alone, while Gunicorn is usually used to run WSGI applications. However, when you use them together, there are some specific benefits:

1. Stability: As a long-standing and well-tested project, Gunicorn’s stability and robustness are guaranteed. By combining Gunicorn with Uvicorn, you canleverage the management capabilities and stability of Gunicorn with the asynchronous performance of Uvicorn.
2.Process Management:Gunicorn provides some powerful process management functions, such as starting, stopping and restarting worker processes. It can better manage resources whensystem load increases, features that Uvicorn may not have when running alone.
3. Simplification of deployment: For organizations that are already accustomed to using Gunicorn to deploy WSGI applications, using Gunicorn with Uvicorn can simplify the process of migrating to ASGI. Teams can leverage existing Gunicorn deployment, monitoring and logging infrastructure.
4. Configuration flexibility: Gunicorn provides many configuration options and plug-ins to easily customize the behavior of the application. When combined with Uvicorn, you can take advantage of these flexibility and allow running both WSGI and ASGI applications on the same machine.
5.Load balancing: Gunicorn is capable of load balancing among multiple worker processes. This not only enables better utilization of multi-core CPUs, but also increases the scalability and redundancy of the system.

In summary, while Uvicorn has advantages on its own, combining Gunicorn with Uvicorn provides additional stability, flexibility, and management capabilities. This combination may be particularly useful in production environments with high volumes of traffic and the need for high availability.

refer to:

FastAPI + NGINX + Gunicorn: Teach you step by step to deploy a high-performance Python web application

Lightning-fast asynchronous server Uvicorn

Gunicorn and Uvicorn