Pyjo.Server.Prethread - Pre-threading non-blocking I/O HTTP and WebSocket server

from Pyjo.Server.Prethread import Prethread

prethread = Prethread(listen=['http://*:8080'])


@prethread.unsubscribe('request').on('request')
def request(prethread, 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()


prethread.run()

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

import Pyjo.Server.Prethread

prethread = Pyjo.Server.Prethread.new(listen=['http://*:8080'])


@prethread.unsubscribe('request').on('request')
def request(prethread, 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()


prethread.run()

Pyjo.Server.Prethread is a full featured pre-threading non-blocking I/O HTTP and WebSocket server, which works as Pyjo.Server.Prefork does, with threads of one process for the worker processes. Each worker is a copy of the server in a thread of its own, with the Pyjo.IOLoop.singleton of the thread and the listen sockets of the manager. Note that the server uses signals for process management, so you should avoid modifying signal handlers in your applications.

The workers share the memory of the process and the application that the manager has loaded, so the application has to be safe to use in several threads at the same time. Python runs one thread at a time where it has the global interpreter lock, so that the workers run at the same time only while they wait, for the network or a blocking call, and on all processor cores on a free-threaded Python. A worker that does not stop when it is told to, as one in a blocking call, cannot be killed, as a process can, so the manager leaves it, and it ends with the process.

The workers need no os.fork(), so the server runs on Windows too, where Pyjo.Server.Prefork does not.

Manager signals

The Pyjo.Server.Prethread manager process can be controlled at runtime with the following signals.

INT, TERM

Shut down server gracefully. The manager and the workers close the listen sockets, so that a new connection is refused, and the workers finish the accepted ones, after shutdown_delay, while the workers serve as before; on Windows, where the workers share the sockets of the manager, a new connection waits until the server ends. A second INT, as of Ctrl-C pressed twice, shuts it down immediately, also during the delay, a second TERM does nothing.

QUIT

Shut down server immediately. On Windows, which has no QUIT, BREAK does it, as of Ctrl-Break.

HUP

Restart the workers gracefully, or emit the event reload when it has subscribers.

TTIN

Increase worker pool by one.

TTOU

Decrease worker pool by one.

The manager does not handle CHLD, so that the application gets the exit status of its child processes.

Windows has no HUP, TTIN and TTOU, and a TERM from another process terminates the process there without its handler, so Ctrl-C and Ctrl-Break of the console are what shut the server down.

Events

Pyjo.Server.Prethread inherits all events from Pyjo.Server.Prefork, which pass the id of the thread of a worker, as threading.get_native_id() gives it, where Pyjo.Server.Prefork passes its process id.

prethread.on('spawn', lambda prethread, id: print(f'Worker {id} started'))

See also

Pyjoyment.Command.prethread, Pyjo.Server.Prefork, Pyjoyment, https://mojolicious.org.

It has no counterpart in Mojolicious; it is inspired by Pyjo.Server.Prefork, the counterpart of Mojo::Server::Prefork, as Thrall is by Starlight.

Classes

class Pyjo.Server.Prethread.Pyjo_Server_Prethread(*, pid_file: str | PathLike[str] | None = None, **kwargs: Any)[source]

Pyjo.Server.Prethread inherits all attributes and methods from Pyjo.Server.Prefork, with prethread.pid in a temporary directory for the default of pid_file.