English 中文(简体)
Python Falcon - WSGI vs ASGI
  • 时间:2024-12-22

Python Falcon - WSGI vs ASGI


Previous Page Next Page  

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 apppcations, to be implemented by web server software for handpng requests from Python-based web apppcation. WSGI specifications were first introduced in 2003 (PEP 333) and later updated in 2010 (PEP 3333).

A WSGI Apppcation 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 apppcation 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 apppcation object must return an iterator consisting of a single byte string.


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

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

The asyncio module provides the abipty to incorporate in Python apppcations 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 apppcations 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 pke 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 capabipties to Python web servers, apppcations and frameworks.

An ASGI apppcation 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 cpent

    Receive − Another asynchronous callable. The apppcation can receive event messages from the cpent.

Following is the prototype of a simple ASGI apppcation 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