ASGI presentation

Original link: https://asgi.readthedocs.io/en/latest/introduction.html

ASGI (Asynchronous Server Gateway Interface, Asynchronous Server Gateway Interface) is the successor WSGI, Python in order to regulate the communication between a network server, and application support asynchronous frame customized.
Compared to the WSGI specification defines the communication between Python application synchronization, ASGI while the communication specifications include synchronous and asynchronous applications, and is backward compatible to follow WSGI applications, services and frameworks.

Introduction

ASGI is WSGI inheritance, was the python network servers, applications, and communication standards frame length connection.
WSGI very successful in python freedom and innovation network applications, and the application of asynchronous Python ASGI hope of advancing on this basis.

Why WSGI inappropriate

You may ask, "Why did you choose not to update WSGI"? This question is often asked in the past many years, the reason is often attributed to a single call WSGI interface is not suitable for this level of involvement of higher WebSocket protocol.
WSGI applications are word, synchronous call, they return only after receiving a request response, this model does not support long connections such as HTTP long polling or WebSocket connection.
Even if we change it to an asynchronous call, initiated the request mode is still single, so those agreements will be many times the incoming event can not call these applications.

ASGI works

ASGI communication by the asynchronous application to individual. Includes scope, contains all the information of the incoming request; send, a method for transmitting an asynchronous event to a client receive, a method for receiving asynchronous events to the client terminal hair.
ASGI not only to accept or multiple applications can send events, and can be combined application while the processor when the coroutine his task (such as an external trigger event to listen, just as Redis queue).
One of the most simple application as a function of the same:

async def application(scope, receive, send):
    event = await receive()
    ...
    await send({"type": "websocket.send", ...})

Each event is predetermined by formatting python dictionary, which forms the basis of the format specification of communication, information can be exchanged between the application server and.
Each event is a typeproperty that structure shows the event. From such receivemay receive event structure a method for transmitting HTTP response beginning:

{
    "type": "http.response.start",
    "status": 200,
    "headers": [(b"X-Header", b"Amazing Value")],
}

You may also be passed to the event structure of the sendmethod used to send WebSocket information:

{
    "type": "websocket.send",
    "text": "Hello world!",
}

WSGI compatible

ASGI is designed to be a superset of WSGI, has a clearly defined method for converting between the two, (provided in asgiref library) by converting decorator can make WSGI applications running within ASGI server. Outside the asynchronous event loop will have a pool of threads for synchronization run WSGI applications.

Guess you like

Origin blog.csdn.net/JosephThatwho/article/details/102756363