Pyjo.App.WSGI - WSGI application

from Pyjo.App.WSGI import WSGI


def hello(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return [b'Hello World!']


app = WSGI(app=hello)

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

import Pyjo.App.WSGI

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

# Mount it in an application of Pyjoyment
import Pyjoyment

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

Pyjo.App.WSGI is an application of Pyjo that runs a WSGI application, as PEP 3333 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 SCRIPT_NAME, after the base path of the request, and the rest of the path is PATH_INFO. A route that embeds the application at a part of the path that does not end with a slash, as /php of /phpx, which SCRIPT_NAME cannot be, does not find it. The body of the request is wsgi.input, of the length of CONTENT_LENGTH, also when the request was chunked.

A WSGI application blocks, and it runs in the event loop, which waits for it, so that the workers of Pyjo.Server.Prefork and Pyjo.Server.Prethread serve it at the same time, as for any blocking operation, with max_clients as low as 1. It is called, and its response iterated and closed, with Pyjo.IOLoop.blocking(), so that it sees no running loop of asyncio, as it would on a WSGI server, and the ORM of Django and a blocking request of Pyjo.UserAgent run on Pyjo.Reactor.Asyncio too. A response that the application has given whole is written with its Content-Length. Another one is written as the application gives it, each chunk in a tick of the event loop once the one before has been written, and after an empty chunk, which the application gives while it has none yet, in the next cycle of the event loop, with the chunked transfer encoding when the application does not give its Content-Length.

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

Attributes

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

See also

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

It has no counterpart in Mojolicious, it is inspired by Mojolicious::Plugin::MountPSGI of CPAN; it is the reverse of Pyjo.Server.WSGI.

Classes

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

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

app: Callable[[dict[str, Any], StartResponse], Iterable[bytes]] | None
application = app.app
app.app = application

The WSGI application to run.

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.

server(server: Pyjo_Server) → None[source]
app.server(Pyjo.Server.Prefork.new())

Called by the server before it starts, and by Pyjoyment.Plugin.Mount for the application that it mounts, so that wsgi.multithread, wsgi.multiprocess and wsgi.run_once say how the server runs the application: in the threads of the workers of Pyjo.Server.Prethread, in the processes of the workers of Pyjo.Server.Prefork, or in a process for each request of Pyjo.Server.CGI. Under a WSGI server, as Pyjo.Server.WSGI runs an application of Pyjoyment, they are those of the environment of the request, which the WSGI server sets.