---
# System prepended metadata

title: ASGI

---

# ASGI

[Doc](https://asgi.readthedocs.io/en/latest/introduction.html)
[Github](https://github.com/django/asgiref)

ASGI是進階的,可以執行非單純線程,並且能兼容部份WSGI

## Github readme
### Function wrappers
兩種
- AsyncToSync lets a synchronous subthread stop and wait while the async function is called on the main thread's event loop, and then control is returned to the thread when the async function is finished.
- SyncToAsync lets async code call a synchronous function, which is run in a threadpool and control returned to the async coroutine when the synchronous function completes.

## Doc Read
### Specifications
#### ASGI (Asynchronous Server Gateway Interface) Specification
針對WSGI的缺點做改進
HTTP/2,WebSocket

ASGI consists of two different components:
- A protocol server, which terminates sockets and translates them into connections and per-connection event messages.
- An application, which lives inside a protocol server, is called once per connection, and handles event messages as they happen, emitting its own event messages back when necessary.

Unlike WSGI, there are two separate parts to an ASGI connection:
- A connection scope, which represents a protocol connection to a user and survives until the connection closes.
- Events, which are messages sent to the application as things happen on the connection, and messages sent back by the application to be received by the server, including data to be transmitted to the client.

https://asgi.readthedocs.io/en/latest/specs/www.html