WSGI is a specification that describes the communication between web servers and Python web applications or frameworks
. It explains how a web server communicates with python web applications/frameworks and how web applications/frameworks can be chained for processing a request.
let us assume a case scenario where you have a web application developed in Django or Flask application.
Since a web application is deployed in the web server. The figure below represents the web server that obtains requests from various users.
The above web server can be apache, NGINX, etc. server which is responsible for handling various static files and caching purposes. Furthermore, you can also use the server as a load balancer if you are willing to scale multiple applications.
So, now a problem arises as a web server has to interact with a Python application.
Hence, a mediator is required for carrying out the interaction between the web servers and the Python application. So, the standard for carrying out communication between the web server and Python application is WSGI(Web Server Gateway Interface).
Now, web server is able to send requests or communicate with WSGI containers. Likewise, Python application provides a ‘callable’ object which contains certain functionalities that are invoked by WSGI application which are defined as per the PEP 3333 standard
. Hence, there are multiple WSGI containers available such as Gunicorn, uWSGI, etc.
Hence, a WSGI container is required to be installed in the project so that a web server can communicate to a WSGI container which further communicates to the Python application and provides the response back accordingly. Finally, when the web server obtains the response, it is sent back to the web browser/users.
If you directly point your web server to your application, it reduces the flexibility of your application.
WSGI gives you flexibility
. Application developers can swap out web stack components for others.
For example, a developer can switch from Green Unicorn to uWSGI without modifying the application or framework that implements WSGI. From : PEP3333.
The availability and widespread use of such an API in web servers for Python [...] would separate choice of framework from choice of web server, freeing users to choose a pairing that suits them, while freeing framework and server developers to focus on their preferred area of specialization.
WSGI promotes scalability
.
Serving thousands of requests for dynamic content at once is the domain of WSGI servers, not frameworks. WSGI servers handle processing requests from the web server and deciding how to communicate those requests to an application framework's process. The segregation of responsibilities is important for efficiently scaling web traffic.
WSGI Server
: A WSGI server is a Python program or library that implements the WSGI specification. It acts as an intermediary between the web server and the Python application. Popular WSGI server implementations include Gunicorn, uWSGI, and mod_wsgi (for Apache).WSGI Application
: The Python web application or framework (e.g., Django) provides a WSGI-compliant application object, often called the "WSGI application." This object must implement a callable that accepts two arguments: the environment and a start_response function.Web Server Configuration
: The web server (e.g., Nginx, Apache) is configured to forward incoming HTTP requests to the WSGI server, typically running on a separate port or socket.Request Handling
:- a. When a client sends an HTTP request to the web server, the web server forwards the request to the WSGI server.
- b. The WSGI server creates an environment dictionary containing information about the request, such as HTTP headers, method, path, and other metadata.
- c. The WSGI server calls the WSGI application callable, passing the environment dictionary and the start_response function as arguments.
Application Processing
:- a. The WSGI application processes the request using the information in the environment dictionary.
- b. The application generates the response data and calls the start_response function to initiate the response.
- c. The application returns an iterable (e.g., a list) containing the response body.
Response Handling
:- a. The WSGI server receives the response data from the application and sends it back to the web server.
- b. The web server sends the response back to the client, usually after performing any necessary transformations or modifications.
By using WSGI, web servers and Python applications can communicate and work together seamlessly. The WSGI server acts as a bridge, translating the HTTP request data from the web server into a format that the Python application can understand, and vice versa for the response.
This separation of concerns allows web servers to focus on handling low-level network tasks efficiently, while Python applications can focus on application logic and business rules. Additionally, WSGI provides a standard interface, enabling different web servers and Python web frameworks to be easily interchangeable.
Concurrency and Parallelism
: Django's development server is single-threaded and can only handle one request at a time. WSGI servers like Gunicorn and uWSGI are designed to handle multiple requests concurrently using a pre-fork multiprocessing model or an asynchronous event loop. This allows them to take advantage of multiple CPU cores and serve requests more efficiently, improving overall throughput and responsiveness.Process Management and Scaling
: WSGI servers provide robust process management capabilities, allowing you to control the number of worker processes and threads, and easily scale up or down based on demand. This makes it easier to handle high traffic loads and efficiently utilize system resources.Static File Serving
: Django's development server serves static files (CSS, JavaScript, images) directly, which can become a bottleneck for performance under high loads. In production, it's recommended to serve static files directly from a dedicated web server like Nginx or Apache, which are optimized for this task. WSGI servers enable this separation of concerns, offloading static file serving to a more efficient web server, and focusing on serving dynamic content.Performance Optimizations
: WSGI servers like Gunicorn and uWSGI are designed for production use and incorporate various performance optimizations. They can take advantage of techniques likepre-forking, event-based I/O, and worker process management
to improve efficiency and reduce overhead.Load Balancing and Clustering
: WSGI servers can be easily integrated with load balancing solutions.Security and Robustness
: WSGI servers are designed to be more robust and secure than Django's development server. They offer features like automatic process monitoring, automatic restarts in case of failures, and better handling of long-running requests or connections.Integration with Web Servers
: WSGI servers can be easily integrated with production-grade web servers like Nginx or Apache, which act asreverse proxies
and handle tasks like SSL termination, load balancing, and caching more efficiently.
While Django's built-in development server is useful for local development and testing, it is not optimized for production workloads and high concurrency.