Pyjo.Server.ASGI - ASGI server

from Pyjo.Server.ASGI import ASGI

asgi = ASGI()
asgi.unsubscribe('request')

@asgi.on('request')
def request(asgi, tx):
    # Request
    method = tx.req.method
    path = tx.req.url.path

    # Response
    tx.res.code = 200
    tx.res.headers.content_type = 'text/plain'
    tx.res.body = f'{method} request for {path}!'.encode()

    # Resume transaction
    tx.resume()

app = asgi.to_asgi_app()

# Or import the module, as in Google Python Style Guide

import Pyjo.Server.ASGI

asgi = Pyjo.Server.ASGI.new()
asgi.unsubscribe('request')

@asgi.on('request')
def request(asgi, tx):
    # Request
    method = tx.req.method
    path = tx.req.url.path

    # Response
    tx.res.code = 200
    tx.res.headers.content_type = 'text/plain'
    tx.res.body = f'{method} request for {path}!'.encode()

    # Resume transaction
    tx.resume()

app = asgi.to_asgi_app()

# uvicorn myapp:app
# hypercorn myapp:app

Pyjo.Server.ASGI allows Pyjoyment applications to run on all ASGI compatible servers, with HTTP, WebSocket and lifespan connections.

The application runs in the event loop of the ASGI server: the reactor of Pyjo.IOLoop follows the loop that is running, so timers, promises and the I/O of Pyjoyment share it, and the response can be given later than the request event, once the transaction is resumed.

An HTTP request is read from the connection scope as the CGI environment that Pyjo.Message.Request.Pyjo_Message_Request.parse() takes, as Pyjo.Server.WSGI does. The ASGI server frames the response itself, so chunked content is sent without its framing and without the Transfer-Encoding header, and the Date header is the one of the ASGI server, unless the application sets one. A WebSocket handshake is a Pyjo.Transaction.WebSocket, as with Pyjo.Server.Daemon; a response other than 101 rejects it, the ASGI server negotiates permessage-deflate on its own, and a Ping frame, which ASGI cannot send, is answered with a Pong frame, as the ASGI server pings the client itself.

The lifespan of the ASGI server is the one of the server of the application: its startup calls Pyjo.App.Pyjo_App.server(), which emits the hook before_server_start of Pyjoyment, and its shutdown calls Pyjo.App.Pyjo_App.server_stopped(), which emits the hook after_server_stop, and waits for the promise that it returns, as of the lifespan of a mounted ASGI application. An exception of either fails the lifespan of the ASGI server with its message.

This needs a reactor that runs in the event loop of the ASGI server and gives awaitables for it. Pyjo.Reactor.Asyncio, the default, does so in an ASGI server on asyncio, whose loop may be another implementation of the event loop of asyncio, as uvloop is, as the reactor takes the loop that the server runs. Pyjo.Reactor.Poll runs a loop of its own, which nothing runs in an ASGI server, so a response given later than the request event fails, with Task got bad yield in a server on asyncio. A server on another framework, as Hypercorn on Trio, needs a reactor for it, which may come from another distribution, as Pyjo.Reactor describes.

ASGI servers

  • uvicorn, on asyncio or uvloop: HTTP, WebSocket and lifespan.

  • Hypercorn, on asyncio or uvloop: HTTP, WebSocket and lifespan; on Trio only with a reactor for Trio, which Pyjoyment does not have.

  • Daphne: HTTP and WebSocket. It adds no Date header, so a response has one only when the application sets it, and it sends no reason with a WebSocket close code. It has no lifespan, so the start and the end of the server are not emitted, and the lifespan of a mounted ASGI application does not shut down.

  • Granian: HTTP, WebSocket and lifespan. A websocket.close that the application sends is not delivered while a receive is waiting, and an application of WebSocket always waits for the next message, so a WebSocket that the application closes stays open until the client closes it.

Events

Pyjo.Server.ASGI inherits all events from Pyjo.Server.

See also

Pyjoyment, Pyjo.Server.WSGI, https://asgi.readthedocs.io.

It has no counterpart in Mojolicious; Pyjo.Server.WSGI is the counterpart of Mojo::Server::PSGI.

class Pyjo.Server.ASGI.Pyjo_Server_ASGI(*, app: Application | None = None, reverse_proxy: bool | None = None, trusted_proxies: list[str] | None = None)[source]

Pyjo.Server.ASGI inherits all attributes and methods from Pyjo.Server and implements the following new ones.

async run(scope: Scope, receive: Receive, send: Send) → None[source]
await asgi.run(scope, receive, send)

Run ASGI for one connection.

to_asgi_app() → Application[source]
app = asgi.to_asgi_app()

Turn the application into an ASGI application.