Pyjo.App.FastCGI - FastCGI application

from Pyjo.App.FastCGI import FastCGI

app = FastCGI(url='fcgi://127.0.0.1:9000')

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

import Pyjo.App.FastCGI

app = Pyjo.App.FastCGI.new(url='fcgi://127.0.0.1:9000')

# PHP on php-fpm, on a UNIX domain socket, with its script
app = Pyjo.App.FastCGI.new(
    url='fcgi+unix://%2Frun%2Fphp%2Ffpm.sock',
    env={'SCRIPT_FILENAME': '/var/www/index.php', 'DOCUMENT_ROOT': '/var/www'},
)

# Mount it in an application of Pyjoyment
import Pyjoyment

pyjoyment = Pyjoyment.new()
pyjoyment.plugin('Mount', {'/php': app})
pyjoyment.plugin('Mount', {'/fcgi': 'fcgi://127.0.0.1:9000'})

Pyjo.App.FastCGI is an application of Pyjo that runs a FastCGI application, as php-fpm runs PHP, through a connection to it, as a client of FastCGI 1.0 in the role of a responder, 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 parameters of the request are its environment of CGI: the part of the path that a route embeds the application under is SCRIPT_NAME, after the base path of the request, which a part of the path that does not end with a slash, as /php of /phpx, cannot be, so that the application is not found there, the rest of the path is PATH_INFO, / for the path of the mount itself, as an empty one is the whole path in SCRIPT_NAME for a server of FastCGI, REQUEST_URI is the path with the query, without the header Proxy, whose HTTP_PROXY an application may take for the proxy of its own requests, as with httpoxy, and env adds to them, as SCRIPT_FILENAME for php-fpm, which document_root gives for each request otherwise. The body of the request goes in records STDIN, each once the one before has been written.

The connection is one for each request, through the event loop, which the application closes after the response, or with max_connections, which it keeps open, kept for the next request of the same event loop. A response of CGI that comes whole, with the end of the request, is written with its Content-Length. Another one is written as it comes, each chunk once the one before has been written to the client, with the chunked transfer encoding when it does not give its Content-Length. What the application writes to its standard error is logged.

A connection that fails or closes before the response is a response 502, and one that closes after the start of the response closes the connection of the client, as one that is quiet for longer than inactivity_timeout does, which, until the response starts, the connection of the client waits for without the inactivity timeout of the server.

Attributes

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

See also

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

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

Classes

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

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

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

Close the idle connections to the application that are kept, and the connections and the servers of ua, as the end of a with block does.

connect_timeout: float | None
timeout = app.connect_timeout
app.connect_timeout = 5

Maximum amount of time in seconds establishing a connection to the application may take before getting canceled, the parameter connect_timeout of the query of url or 10.

document_root: str | PathLike[str] | None
root = app.document_root
app.document_root = '/var/www'

Directory of the scripts of the application, as of PHP, as the application sees it, which takes the script of each request from its path, as a web server does: the path after the mount, up to its first part that ends with one of script_extensions, names the script, whose file in the directory is SCRIPT_FILENAME and whose path after the mount is SCRIPT_NAME, the rest of the path is PATH_INFO, and a path that ends with a slash names index with the first of them in it, as index.php. A path with .. is not found. With script, a path that names no script of the directory is the one of script, as the one entry of an application that takes every path, as with try_files of nginx, which the path decides, not the files, which the application may see elsewhere. SCRIPT_FILENAME of env takes its place.

env: dict[str, str]
env = app.env
app.env = {'SCRIPT_FILENAME': '/var/www/index.php'}

Parameters that are added to the environment of CGI of each request, and replace the ones of the same name.

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.

inactivity_timeout: float | None
timeout = app.inactivity_timeout
app.inactivity_timeout = 300

Maximum amount of time in seconds a connection to the application can be inactive before it gets closed, the parameter inactivity_timeout of the query of url or 60. Until the response starts, the connection of the client waits for it without the inactivity timeout of the server, so that this one gives the response 502 to an application that takes longer.

max_connections: int | None
max = app.max_connections
app.max_connections = 4

Maximum number of idle connections to the application that are kept and used again by the requests of the same event loop, the parameter max_connections of the query of url or 0, which closes each connection after its request. The requests ask the application to keep the connection, as fastcgi_keep_conn of nginx does, and an idle connection that the application closes, or that is inactive for longer than inactivity_timeout, is closed and left out. A connection that is used again and closes before the response gets the request again on a new one.

php-fpm binds a worker to a connection for as long as the connection is open, also while it is idle, so that each connection that is kept takes one of the pm.max_children of the pool of php-fpm, for each event loop, as of each worker of Pyjo.Server.Prefork: too many of them leave no worker to the other clients of the pool.

public_source: bool | None
boolean = app.public_source
app.public_source = True

The static files of the application that mounts this one, as the public directory of Pyjoyment.Static, may have the scripts of document_root or script on purpose, the parameter public_source of the query of url, false by default, with which server() fails, as those static files are served before the route of the mount, so their source instead of their output.

script: str | None
script = app.script
app.script = '/var/www/index.php'
app.script = 'index.php'

Script of the application for every path, as the one entry of an application of PHP that takes every path, with the path after the mount as PATH_INFO, or for every path that names no script of document_root, in which it is then.

script_extensions: list[str] | None
extensions = app.script_extensions
app.script_extensions = ['.php', '.phtml']

Extensions of the names of the scripts of document_root, the parameters script_extensions of the query of url, or ['.php'].

sendfile: Sendfile | None
places = app.sendfile
app.sendfile = '/var/www/private'
app.sendfile = ['/var/www/private', '/var/www/media']
app.sendfile = re.compile(r'/var/www/[^/]+/private/.*')

Directories of the files that the application may reply with by the header X-Sendfile, as Apache and lighttpd take it, with the path of the file, absolute or in one of them, or regular expressions of the whole path of such a file, once its symbolic links are resolved, as the private files that are not in document_root, the parameters sendfile of the query of url, which are directories, or none, which passes the header on as it is. A route that embeds the application replies then with the file, as the helper reply.file of Pyjoyment does, with the status and the other headers of the application, and a file that they do not allow is not found.

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

Fail before the server starts when the static files of the application that mounts this one, as the public directory of Pyjoyment.Static, have the scripts of document_root or script on the same file system, unless public_source is true.

static: bool | None
boolean = app.static
app.static = True

Reply with a file of document_root for a path that names it and no script, as try_files of nginx does, as the helper reply.file of Pyjoyment does, with ranges and conditional requests, the parameter static of the query of url, false by default. The files are those that Pyjoyment sees, as for an application on the same file system, and a route that embeds the application replies with them, as Pyjoyment.Plugin.Mount makes.

url: str | Pyjo_URL | None
url = app.url
app.url = 'fcgi://127.0.0.1:9000'
app.url = 'fcgi+unix://%2Frun%2Fphp%2Ffpm.sock'

Location of the application, a TCP/IP address and port of fcgi:// or the path of a UNIX domain socket of fcgi+unix://, escaped as the listen locations of Pyjo.Server.Daemon are. Its query can give connect_timeout, document_root, inactivity_timeout, max_connections, public_source, script, script_extensions, sendfile and static, as the listen locations give their options, which the attributes take the place of.

app.url = 'fcgi://127.0.0.1:9000?document_root=%2Fvar%2Fwww&script=index.php'
app.url = 'fcgi://127.0.0.1:9000?inactivity_timeout=300&max_connections=4'