Code-Memo

WSGI (Web Server Gateway Interface) and ASGI (Asynchronous Server Gateway Interface) are interfaces between web servers and web applications or frameworks, designed to promote a standard way of handling requests and responses in Python web development.

WSGI:

How it Works:

Components:

  1. WSGI Server: The server that receives requests from clients and forwards them to the application. Examples include Gunicorn, uWSGI, and mod_wsgi.
  2. WSGI Application: The actual web application or framework (like Django) that processes the request.

Usage in Django:

Example wsgi.py:

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

application = get_wsgi_application()

Pros:

Cons:

ASGI:

How it Works:

Components:

  1. ASGI Server: Servers that support ASGI, such as Daphne, Uvicorn, and Hypercorn.
  2. ASGI Application: The application or framework that processes the request asynchronously.

Usage in Django:

Example asgi.py:

import os
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

application = get_asgi_application()

Pros:

Cons:

Choosing Between WSGI and ASGI

When to Use WSGI:

When to Use ASGI:

Deployment Considerations

WSGI Deployment:

ASGI Deployment:

Middleware: