web application - chunhualiao/public-docs GitHub Wiki

where to deploy your web apps?

  • hugging face spaces
  • youware
  • what else

Gunicorn (Green Unicorn) is a Python WSGI HTTP Server for UNIX.

In simpler terms, it's a piece of software that acts as an intermediary between your Python web application (like the Flask app we have) and the web server (like Nginx, or in our Docker case, directly serving HTTP requests).

Here's why it's used and what it does:

  1. WSGI (Web Server Gateway Interface): This is a standard interface between web servers and Python web applications. Flask, Django, and other Python web frameworks are WSGI-compatible. Gunicorn implements the server side of this interface.

  2. Production-Ready:

    • Development vs. Production: When you run a Flask app directly using python app.py, it uses Flask's built-in development server. This server is great for development because it's simple and provides helpful debugging features. However, it's not designed for production environments. It's typically single-threaded and can't handle many concurrent requests efficiently.
    • Gunicorn's Role: Gunicorn is built for production. It can handle multiple requests concurrently by running multiple worker processes. This makes your application more robust, scalable, and performant under load.
  3. Concurrency: Gunicorn manages multiple worker processes (as specified by --workers 3 in our Dockerfile). Each worker can handle a request, allowing your application to process several requests at the same time, rather than one after another.

  4. Stability and Reliability: It provides features like graceful restarts, process management, and error handling that are crucial for a stable production environment.

  5. Simplicity: While powerful, Gunicorn is relatively simple to configure and use compared to some other production servers.

In our Dockerfile, the CMD instruction CMD ["/app/venv/bin/gunicorn", "--workers", "3", "--bind", "0.0.0.0:5001", "app:app"] tells the Docker container to start Gunicorn, which then serves your Flask application (app:app) on port 5001, using 3 worker processes. This ensures your application is ready for production-like traffic when deployed on Hugging Face Spaces.