Pyjo.App.ASGI - ASGI application

from Pyjo.App.ASGI import ASGI


async def hello(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!'})


app = ASGI(app=hello)

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

import Pyjo.App.ASGI

app = Pyjo.App.ASGI.new(app=hello)

# Mount it in an application of Pyjoyment
import Pyjoyment

pyjoyment = Pyjoyment.new()
pyjoyment.plugin('Mount', {'/hello': app})

Pyjo.App.ASGI is an application of Pyjo that runs an ASGI application, as https://asgi.readthedocs.io describes it, so that a server of Pyjo serves it, a route of Pyjoyment embeds it, as Pyjoyment.Plugin.Mount does, and Pyjo.Test.Pyjo tests it.

The part of the path that a route embeds the application under is root_path, after the base path of the request, and path is the whole path, with root_path in front of it. A route that embeds the application at a part of the path that does not end with a slash, as /php of /phpx, which root_path cannot be, does not find it. The body of the request is the message http.request of receive, in chunks with more_body, and the next receive waits for the end of the connection, the message http.disconnect.

The application runs as a coroutine of the reactor, as Pyjo.Reactor.Pyjo_Reactor.run_coroutine() runs it: with Pyjo.Reactor.Asyncio, a task of the event loop of asyncio, where it can await anything of the loop, and with another reactor, as Pyjo.Reactor.Poll, a coroutine that awaits only receive and send, so that an application that awaits anything else fails there. A response that the application gives in one message http.response.body is written with its Content-Length. Another one is written as the application sends it, with the chunked transfer encoding when the application does not give its Content-Length, and send returns once the chunk has been written, so that a stream of server-sent events goes at the pace of the client. Once the connection is gone, send does nothing.

An exception of the application before its response starts is a response 500, the exception page of Pyjoyment in a route that embeds it, an exception after that is logged and closes the connection, as does an application that returns before the end of its response, and an exception after the end of the response is logged.

A WebSocket handshake is the connection websocket, whose subprotocols are the ones that the client offers. The response of the handshake waits for the message websocket.accept, whose subprotocol and headers are those of the response 101, and a websocket.close before it rejects the handshake with the response 403, as does a response of the application with the extension websocket.http.response. The messages of the WebSocket are websocket.receive, and send of websocket.send returns once the message has been written. The end of the WebSocket, also of a connection that is gone before the response of its handshake, is the message websocket.disconnect with the status of the close, after which send does nothing. An exception of the application, or its return, before the response of the handshake is a response 500, and after it is logged and closes the WebSocket with the status 1011, unless the application has closed it.

The WebSockets are pinged, as by an ASGI server, with Pyjo.Transaction.WebSocket.Pyjo_Transaction_WebSocket.heartbeat() and the intervals of ping_interval and ping_timeout, so that the inactivity timeout of the server does not close a WebSocket without messages, and a client that is gone is found.

The lifespan of the application, the connection lifespan, starts with the first request in each event loop, as of each process of Pyjo.Server.Prefork and each thread of Pyjo.Server.Prethread, or with startup(), and the requests wait for its lifespan.startup.complete, after which each gets a copy of its state. A lifespan.startup.failed is logged, and the requests get the response 500. An application that fails or returns before its startup has no lifespan, as uvicorn takes it, and one that neither answers nor fails keeps the requests waiting. The lifespan shuts down once the server has stopped gracefully, with server_stopped(), which Pyjoyment.Plugin.Mount calls with the hook after_server_stop, with close(), as at the end of a with block, and at the end of the interpreter, where nothing has shut it down.

Attributes

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

See also

Pyjoyment, Pyjoyment.Plugin.Mount, Pyjo.App.WSGI, Pyjo.Server.ASGI, https://mojolicious.org.

It has no counterpart in Mojolicious; it is the reverse of Pyjo.Server.ASGI.

Classes

class Pyjo.App.ASGI.Pyjo_App_ASGI(attrs: dict[str, Any] | None = None, /, **kwargs: Any)[source]

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

app: Application | None
application = app.app
app.app = application

The ASGI application to run.

close() → None[source]
app.close()

Shut the lifespan of the application down, and wait for it, unless the event loop runs, as the end of a with block does, and close the connections and the servers of ua.

handler(tx: Any) → None[source]
app.handler(Pyjo.Transaction.HTTP.new())
app.handler(Pyjoyment.Controller.new())

Run the application for the request of the transaction, or of the controller of a route that embeds the application, and write its response.

ping_interval: float | None
interval = app.ping_interval
app.ping_interval = 60

Interval in seconds of the pings of the WebSockets, defaults to 20, as of uvicorn. None or 0 sends no pings.

ping_timeout: float | None
timeout = app.ping_timeout
app.ping_timeout = 60

Timeout in seconds of the answer of a client to a ping of a WebSocket, after which its connection is closed, defaults to 20, as of uvicorn. None or 0 waits for no answer.

server_stopped(server: Any) → Pyjo_Promise | None[source]
promise = app.server_stopped(Pyjo.Server.Daemon.new())

Shut the lifespan of the application down, as shutdown() does, once the server has stopped.

shutdown() → Pyjo_Promise | None[source]
promise = app.shutdown()

Shut the lifespan of the application in the event loop of the thread down, with the message lifespan.shutdown, and return a promise that is fulfilled once the application has answered it, or None when the lifespan has not started.

startup() → Pyjo_Promise[source]
promise = app.startup()

Start the lifespan of the application in the event loop of the thread, with the message lifespan.startup, unless it has started, and return a promise that is fulfilled once the application has answered it, as the first request does.