Uvicorn & Gunicorn

Uvicorn and Gunicorn

Uvicorn and Gunicorn are important concepts when developing applications in Python.

However, there are many concepts to be aware of in order to fully understand Uvicorn and Gunicorn.

The following is a brief summary of the necessary concepts, and the details will be dealt with separately later.


Necessary Concepts

Starlette

Starlette is a Web application server that can run asynchronously. Starlette runs on top of Uvicorn.

FastAPI

FastAPI provides many features on top of Starlette. If you used only Starlette without FastAPI, you have to implement all data validation and serialization.

Because of that, final application may have same overhead as building using the FastAPI.

And in most cases, data validation and serialization are the highest amounts of code written by an application.

FastAPI saves development time, bugs, and cord-lines, and gives you the same performance (or better performance) when you don't.


Asyncio

Asyncio is library which allows to make asynchronous code with using async/await phrase.

Asyncio is used as the basis for several Python asynchronous frameworks that provide high-performance network and web servers, database connection libraries, and distributed task queues.

Asyncio is best suited for IO bottleneck and high-level structured network code.


Uvloop

Uvloop is an event loop replacement for asyncio, making asyncio faster, and more than twice the performance compared to node.js, gevent, and other Python frameworks.

In “uvloop.readthedocs.io”, describes uvloop as follows.

uvloop is a fast, drop-in replacement of the built-in asyncio event loop. uvloop is released under the MIT license.

uvloop and asyncio, combined with the power of async/await in Python 3.5, makes it easier than ever to write high-performance networking code in Python.

uvloop makes asyncio fast. In fact, it is at least 2x faster than nodejs, gevent, as well as any other Python asynchronous framework. The performance of uvloop-based asyncio is close to that of Go programs.

The uvloop was written in Cython based on libuv. Libuv is a high-performance, multi-platform asynchronous I/O library used by node.js, and is fast and stable.

It is no exaggeration to say that the reason why node.js became popular and widely distributed is because of libuv.


Gunicorn

Gunicorn is WSGI server. WSGI is the promised interface or rule between a Python-written web application and a Python-written server.

Simply put, when WSGI servers and web applications are created in accordance with WSGI’s rules, web applications provide the flexibility to freely select and use WSGI servers regardless of internal implementation.

For example, Django provides the wsgi.py for WSGI, you can freely select the WSGI servers, such as gunicorn, uwsgi, without change of internal implementation.

Gunicorn’s process adopts a process-based processing method(프로세스 기반의 처리 방식), which is largely divided into master process and worker process internally.

When gunicorn runs, the process itself is the master process, and the worker process is created based on the number of workers assigned to the setting using forks.

Master process is responsible for managing the worker process, which imports web applications, and receives requests and forwards them to the web application code for processing.


Uvicorn

Uvicorn is a superfast ASGI Web server. Runs uvloop-based asynchronous Python code in a single process.

Last updated