WSGI vs ASGI for Python Web Development

QAM Chen
3 min readDec 29, 2023

Nowadays, there are many Python Web frameworks including django, flask, fastapi…etc. No matter what you use, you will meet final problem. How do I deploy my code for production Web. Then WSGI and ASGI come into the picture.

Architecture for Web server

Users connect to Nginx using a browser, which serves various purposes such as web serving, reverse proxying, caching, load balancing, and media streaming. Nginx can be referred to as a web server. Subsequently, Nginx communicates with server gateway interfaces, including Web Server Gateway Interface (WSGI) and Asynchronous Server Gateway Interface (ASGI). Finally, the server gateway interface executes functions within application frameworks such as FastAPI, Django, or Flask.

The interaction between the web server and the server gateway interface is managed by a protocol that outlines the guidelines for their communication. In Python, the protocol may be the Web Server Gateway Interface (WSGI) or the Asynchronous Server Gateway Interface (ASGI), depending on whether the application employs synchronous or asynchronous programming.

Web Server Gateway Interface (WSGI)

Originally introduced in 2003 and revised in 2010, WSGI (Web Server Gateway Interface) is a specification that outlines the communication protocol between the web server and a server gateway interface. It defines a straightforward interface for managing HTTP requests and responses, enabling developers to create web applications in Python without the need to delve into the intricacies of the underlying server.

WSGI operates by exposing a Python function to the web server, typically named ‘application’ or ‘app’ by default. This function takes two parameters: ‘environ’ and ‘start_response.’

  • environ: a dictionary is required to contain these web server’s environment variables. For example, REQUEST_METHOD, PATH_INFO, QUERY_STRING…etc.
  • start_response: The callable function used to begin the HTTP response.

The simple WSGI implementation is as below.

def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return [b'Greetings universe']

Django and Flask both utilize WSGI. However, because WSGI servers operate in a synchronous manner, they handle one request at a time, blocking the connection until the result is returned. This limitation can result in scalability issues and an inability to handle WebSocket or long-polling requests.

Asynchronous Server Gateway Interface (ASGI)

ASGI is a more recent specification crafted to facilitate asynchronous programming in Python. Unlike WSGI, which is tailored for synchronous requests, ASGI empowers developers to create web applications that can handle multiple requests concurrently, without impeding the main thread.

To implement ASGI, you can define a function named ‘application’ that accepts three parameters.

  • scope: A dictionary with information about the current request, akin to environ in WSGI, but with a slightly different naming convention for the details.. For example, method, path, headers, query_string…etc.
  • receive: An async callable (function) that lets the application send messages back to the client.
  • send: An async callable that lets the application receive messages from the client..
async def application(scope, receive, send):
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'content-type', b'text/plain'],
],
})

await send({
'type': 'http.response.body',
'body': b'Hello, world!',
})

How to write ASGI server from scratch and integrate it with FastAPI

We do the same implementation based on the document.

Implementation TBD.

Reference

  1. Python Falcon — WSGI vs ASGI from tutorialspoint
  2. Python Falcon — WSGI vs ASGI from geeksforgeeks
  3. ASGI explained: The future of Python web development
  4. Why Use WSGI/ASGI When We Have Nginx?
  5. Writing an ASGI server from scratch and using it with FastAPI
  6. ASGI

--

--

QAM Chen

My career starts as embedded system engineer in HTC. I was eager to learn data technology and studied second master in UCI Computer science. Now, I’m MLE.