search
HomeBackend DevelopmentPython TutorialASGI explained: The future of Python web development

​Translator|Li Rui

Reviewer|Sun Shujuan

Python web applications have long followed the Web Server Gateway Interface (WSGI) standard, which The standard describes how they communicate with web servers. WSGI, originally introduced in 2003 and updated in 2010, relies only on easy-to-implement features that are natively available in Python 2.2. As a result, WSGI was quickly integrated into all major Python web frameworks and became the cornerstone of Python web development.

Fast forward to 2022. Python2 has been deprecated and Python now has native syntax for handling asynchronous operations such as network calls. WSGI and other standards that assume synchronous behavior by default fail to take advantage of the performance and efficiency gains of asynchronous. This in turn means that WSGI cannot handle high-level protocols like WebSocket efficiently.

Enter ASGI, which is the Asynchronous Server Gateway Interface. Similar to WSGI, ASGI describes a common interface between Python web applications and web servers. Unlike WSGI, ASGI allows multiple asynchronous events per application. Additionally, ASGI supports both synchronous and asynchronous applications. Developers can migrate legacy synchronous WSGI web applications to ASGI or build new asynchronous web applications using ASGI.

1. How WSGI works

The working principle of WSGI is to expose Python functions to the web server, usually named as an application or app. This function takes two parameters:

  • #environ: A dictionary containing information about the current request and environment variables provided by the web server.
  • start_response: Will be used to start the function that sends the HTTP response back to the client.

The data returned by the function constitutes the response body.

A simple application function might look like this:​

def application(environ, start_response):

 start_response('200 OK', [('Content-Type', 'text/plain')])

 return [b'Greetings universe']

If you are using a WSGI-compatible web framework (such as Flask ), then the framework itself will provide an application functionality and all its components will be automatically connected.

WSGI has two disadvantages: First, WSGI only handles one request and response at a time and assumes that the response will be returned immediately. There is no way to handle long-held connections, such as WebSocket or long-polling HTTP connections.

Secondly, WSGI is only synchronous. Even with multi-threaded connection pooling, each connection blocks until it returns a response. Many WSGI setups are able to handle thread pools and process pools, but these are limited by the synchronization of the WSGI interface itself.

2. How ASGI works

ASGI is similar in appearance to WSGI. Like WSGI, developers can define an application function object, but it is an asynchronous function with three parameters instead of two:

scope: Contains information about the current request A dictionary of information, similar to environ in WSGI, but the details of the naming convention are slightly different.

send: An asynchronous callable function that allows the application to send messages back to the client.

receive: Asynchronous callable function that allows the application to receive messages from the client.

A simple ASGI application function looks like this:

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!',
 })

Like the WSGI Web framework, the ASGI Web framework will generate its own application () function and connect them as needed.

The most obvious difference from ASGI is the use of asynchronous metaphors throughout the function. The function itself is asynchronous, where the HTTP headers and response body are sent via two separate await send() commands. This way, the functions themselves and their send commands don't block anything; they can be intertwined with the application's calls and sent from many other connections simultaneously.

receive is not used in this example, but it is also an asynchronous function. It allows receiving the request body without blocking other operations. Requests and responses can be passed incrementally to or from the server in this manner - something that cannot be done well, or perhaps at all, with WSGI.

3. Using synchronous and asynchronous functions in ASGI

When using ASGI, you need to use as many asynchronous functions and async-friendly libraries as possible. It pays to get into the habit of using async, as the problems with using synchronous-only code can be serious. Any long call to a synchronous function will block the entire call chain, making the benefits of using async almost gone.

If you encounter problems when using long-running synchronous calls, you need to use asyncio.run_in_executor to outsource the call to a thread pool or process pool. Thread pools should be used whenever waiting for external events or non-CPU-intensive tasks. The process pool should be used for CPU-intensive local tasks.

For example, if there is a route in a web application that can call a remote website, then threads should be used. Or better yet, use the aiohttp library that makes asynchronous HTTP requests. If you want to call the Pillow image library to resize the image, you should probably use run_in_executor with a process pool. Although there is some slight overhead in transferring data back and forth between processes, using run_in_executor does not block other events.

4. Web framework that supports ASGI

By implementing the application() object, ASGI Web applications can be written manually. But in most cases, it's simpler to use an asynchronous native, ASGI-centric Python web framework. Here are some common ASGI-compatible web frameworks:

Starlette and FastAPI: These emerging frameworks (FastAPI is built on top of Starlette) are async-first, so they all support ASGI No wonder. If you are developing web applications from scratch, then they are the most modern and cutting-edge web frameworks for Python.

Quart: While the major Python web framework Flask does support ASGI, Flask is not designed to leverage async metaphors from the inside out. Quart from GitLab uses Flask's syntax and metaphors but allows for asynchronous route handlers.

Django 3.0 and above: Starting from Django 3.0, the prestigious Django web framework supports ASGI. Support for asynchronous code in Django applications was added in Django 3.1, rather than just being able to mount Django on ASGI handlers. For a framework not known for execution speed, the presence of async brings better performance to those who choose to take advantage of it.

Original link: https://www.infoworld.com/article/3658336/asgi-explained-the-future-of-python-Web-development.html​

The above is the detailed content of ASGI explained: The future of Python web development. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
Merging Lists in Python: Choosing the Right MethodMerging Lists in Python: Choosing the Right MethodMay 14, 2025 am 12:11 AM

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

How to concatenate two lists in python 3?How to concatenate two lists in python 3?May 14, 2025 am 12:09 AM

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Python concatenate list stringsPython concatenate list stringsMay 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Python execution, what is that?Python execution, what is that?May 14, 2025 am 12:06 AM

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.