- Python Falcon - Discussion
- Python Falcon - Useful Resources
- Python Falcon - Quick Guide
- Python Falcon - Deployment
- Python Falcon - Testing
- Python Falcon - Sqlalchemy Models
- Python Falcon - Websocket
- Python Falcon - CORS
- Python Falcon - Middleware
- Python Falcon - Hooks
- Python Falcon - Error Handling
- Python Falcon - Status Codes
- Python Falcon - Cookies
- Python Falcon - Jinja2 Template
- Python Falcon - Inspect Module
- Falcon - Suffixed Responders
- Python Falcon - Routing
- Python Falcon - App Class
- Python Falcon - Resource Class
- Request & Response
- Python Falcon - API Testing Tools
- Python Falcon - Uvicorn
- Python Falcon - ASGI
- Python Falcon - Waitress
- Python Falcon - Hello World(WSGI)
- Python Falcon - WSGI vs ASGI
- Python Falcon - Environment Setup
- Python Falcon - Introduction
- Python Falcon - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 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