Python Falcon - WSGI vs ASGI



Web Server Gateway Interface(WSGI)

Some of the most popular Python web frameworks implement WSGI (stands for Web Server Gateway Interface). WSGI is essentially a set of specifications for a universal interface between web server and web applications, to be implemented by web server software for handling requests from Python-based web application. WSGI specifications were first introduced in 2003 (PEP 333) and later updated in 2010 (PEP 3333).

A WSGI Application object is invoked by the server by passing the following arguments −

  • environ − A Python dict object which is similar to CGI environment variables and certain WSGI specific variables.

  • start_response − A callback function to be used by the application to return its response along with headers and status code.

This object can be any callable object as in Python such as a function, method, a class or its instance with __call__() method available to it. This application object must return an iterator consisting of a single byte string.

def application (environ, start_response):
   ...
   ...
   return [("Hello World!".encode("utf-8")]

However, WSGI-enabled servers are synchronous in operation, because of which the applications are not that efficient. Python started asynchronous programming support with version 3.4 by introducing the asyncio module as a part of the standard library.

The asyncio module provides the ability to incorporate in Python applications a style of concurrent programming (which is often called cooperative multitasking). In this approach, the operating system doesn’t obstruct the context switching among different processes. Instead, a process yields periodically to accommodate other processes so that many applications can run simultaneously.

In Python’s version 3.5, these two keywords async and await were added. A Python function defined with the async keyword becomes a coroutine and hence can’t be run like a normal function. Instead, we need to call it using asyncio.run (coroutine). The execution of a coroutine can be made to pause till the completion of another coroutine by the await keyword.

import asyncio
async def main():
   print('hello')
   await asyncio.sleep(5)
   print('world')

asyncio.run(main())

Asynchronous Server Gateway Interface(ASGI)

ASGI stands for Asynchronous Server Gateway Interface (as per its official documentation, it is a spiritual successor to WSGI), it adds the async capabilities to Python web servers, applications and frameworks.

An ASGI application is an asynchronous callable object (a user-defined function or an object of a class having __call__() method). It takes three arguments as follows −

  • Scope − A dict containing details of a specific connection

  • Send − An asynchronous callable, by which event messages can be sent to the client

  • Receive − Another asynchronous callable. The application can receive event messages from the client.

Following is the prototype of a simple ASGI application represented by an asynchronous function −

async def app(scope, receive, send):
   assert scope['type'] == 'http'
   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!',
})
Advertisements