Asynchronous Server Gateway Interface

From English Wikipedia @ Freddythechick

This is the current revision of this page, as edited by imported>Watchduck at 12:00, 5 July 2024 (It is clear from the context, that this is not about the snake.). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
ASGI Specification
Version3.0
DeveloperASGI Team
Release date2019-03-04[1]
Websiteasgi.readthedocs.io/en/latest/specs/index.html
Licensepublic domain[2]
StatusDraft

The Asynchronous Server Gateway Interface (ASGI) is a calling convention for web servers to forward requests to asynchronous-capable Python frameworks, and applications. It is built as a successor to the Web Server Gateway Interface (WSGI).

Where WSGI provided a standard for synchronous Python application, ASGI provides one for both asynchronous and synchronous applications, with a WSGI backwards-compatibility implementation and multiple servers and application frameworks.

Example

An ASGI-compatible "Hello, World!" application written in Python:<syntaxhighlight lang="python3" line="1"> async def application(scope, receive, send):

   event = await receive()
   ...
   await send({"type": "websocket.send", ...})

</syntaxhighlight>Where:

  • Line 1 defines an asynchronous function named <syntaxhighlight lang="Python" inline="1">application</syntaxhighlight>, which takes three parameters (unlike in WSGI which takes only two), <syntaxhighlight lang="Python" inline="1">scope</syntaxhighlight>, <syntaxhighlight lang="Python" inline="1">receive</syntaxhighlight> and <syntaxhighlight lang="Python" inline="1">send</syntaxhighlight>.
    • <syntaxhighlight lang="Python" inline="1">scope</syntaxhighlight> is a <syntaxhighlight lang="Python" inline="1">dict</syntaxhighlight> containing details about current connection, like the protocol, headers, etc.
    • <syntaxhighlight lang="Python" inline="1">receive</syntaxhighlight> and <syntaxhighlight lang="Python" inline="1">send</syntaxhighlight> are asynchronous callables which let the application receive and send messages from/to the client.
  • Line 2 receives an incoming event, for example, HTTP request or WebSocket message. The <syntaxhighlight lang="Python" inline="1">await</syntaxhighlight> keyword is used because the operation is asynchronous.
  • Line 4 asynchronously sends a response back to the client. In this case, it is a WebSocket communication.

Web Server Gateway Interface (WSGI) compatibility

ASGI is also designed to be a superset of WSGI, and there's a defined way of translating between the two, allowing WSGI applications to be run inside ASGI servers through a translation wrapper (provided in the asgiref library). A threadpool can be used to run the synchronous WSGI applications away from the async event loop.

See also

Lua error in mw.title.lua at line 346: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal').

References

  1. ^ "Version History".
  2. ^ "Copyright". GitHub. Retrieved 2022-09-14.

External links