Mastering Python Web Deployment: CGI, FastCGI, WSGI, uWSGI & More
This article explains the key server interfaces and deployment options for Python web applications—including CGI, FastCGI, WSGI, uWSGI, Gunicorn, and Tornado—detailing their architectures, workflows, and practical usage with Nginx and process managers.
Don’t let your server run naked. While PHP can be deployed in minutes, Python web deployment is more complex due to many tools and limited native server support, so understanding core concepts is essential.
CGI
Common Gateway Interface (CGI) is a standard that lets a web server execute external programs and return their output to the browser. It acts as a bridge between HTML requests and server-side scripts, creating a new process for each request, which can cause high resource consumption.
CGI workflow:
Browser requests a CGI URL via a form or hyperlink.
Server launches the specified CGI program.
CGI program processes the request, often using user input.
Program formats the result as an HTML document.
Server returns the document to the browser.
Python provides a cgi module for native CGI support.
FastCGI
FastCGI improves on CGI by keeping interpreter processes alive, eliminating the fork‑and‑execute overhead for each request. It is supported by major servers (Apache, Nginx, Lighttpd) and languages, including Python, and can increase performance by at least five times.
FastCGI workflow:
Web server loads a FastCGI process manager (e.g., PHP‑FPM, spawn‑cgi).
Manager starts multiple CGI interpreter processes and waits for connections.
When a client request arrives, the manager selects a CGI interpreter and forwards the request.
The FastCGI child processes handle the request and return output to the web server.
WSGI
Python Web Server Gateway Interface (WSGI) defines a simple, universal interface between web servers and Python applications or frameworks. It builds on CGI concepts and separates the server (gateway) from the application.
WSGI middleware can:
Rewrite environment variables and route requests.
Run multiple applications in one process.
Provide load balancing and remote processing.
Perform post‑processing such as applying XSLT.
WSGI components are typically arranged as: WSGI Server → (WSGI Middleware)* → WSGI Application.
uWSGI
uWSGI is a versatile server that implements the uwsgi, WSGI, HTTP, and other protocols. It is designed for deploying distributed web applications and supports many languages via plugins.
Key features:
Very high performance.
Low memory usage (about half of Apache mod_wsgi).
Multiple app management.
Extensive logging for performance analysis.
Highly configurable (memory limits, graceful restarts, etc.).
Gunicorn
Gunicorn (Green Unicorn) is a pre‑fork worker model server compatible with many Python frameworks, especially Django. It uses the WSGI protocol, is lightweight, and integrates easily with process managers like supervisord.
Design:
Master process spawns worker processes.
Workers handle requests; master monitors and restarts them.
Supports various worker types (sync, gevent, eventlet, etc.).
Tornado
Tornado is both an asynchronous, non‑blocking web framework and a built‑in HTTP server. When used with other frameworks (e.g., Flask), it can expose a WSGI interface via tornado.wsgi.WSGIContainer.
wsgiref
The standard library module wsgiref provides a simple reference implementation of a WSGI server, used by Django’s development server.
Practical Deployment Choices
For a Django project, a common setup is Nginx + uWSGI; for a Tornado project, Nginx + Gunicorn (managed by supervisord). Nginx handles static content and load balancing, while the Python processes run in multiple workers to work around the GIL.
Source: titanjf, www.cnblogs.com/titanjf/p/python-web-deploy.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
