Pyjo.Server.Reload - Auto-restarting development server

from Pyjo.Server.Reload import Reload

reload = Reload()
reload.run('/home/sri/myapp.py')

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

import Pyjo.Server.Reload

reload = Pyjo.Server.Reload.new()
reload.run('/home/sri/myapp.py')

Pyjo.Server.Reload is a full featured, self-restart capable non-blocking I/O HTTP and WebSocket server, built around the very well tested and reliable Pyjo.Server.Daemon, with IPv6, TLS, SNI, UNIX domain socket, Comet (long polling), keep-alive and multiple event loop support. Note that the server uses signals for process management, so you should avoid modifying signal handlers in your applications.

To start applications with it you can use the pyjo-reload script, or python -m Pyjo.Server.Reload.

$ pyjo-reload ./myapp.py
Web application available at http://127.0.0.1:3000

The server restarts the application whenever a file that it watches changes, in a fork of itself, made with os.fork(), so it runs where os.fork() exists, not on Windows. Name resolution in a thread, SOCKS5 and TLS support can be disabled with the PYJO_NO_NNR, PYJO_NO_SOCKS and PYJO_NO_TLS environment variables.

Signals

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

INT, TERM

Shut down server immediately.

pyjo-reload

Usage: pyjo-reload [OPTIONS] [APPLICATION]

  pyjo-reload ./myapp.py
  pyjo-reload -m production -l https://*:443 -l http://[::]:3000 ./myapp.py
  pyjo-reload -l 'https://*:443?cert=./server.crt&key=./server.key' ./myapp.py
  pyjo-reload -w /usr/local/lib -w public -w myapp.conf ./myapp.py

Options:
  -b, --backend <name>           Reload backend to use for reloading,
                                 defaults to "Poll"
  -h, --help                     Show this message
  -l, --listen <location>        One or more locations you want to listen on,
                                 defaults to the value of PYJO_LISTEN, the
                                 port of PORT or "http://*:3000"
      --log-format <name>        Format of log messages, "text" or "json",
                                 defaults to the value of PYJO_LOG_FORMAT or
                                 "text"
  -m, --mode <name>              Operating mode for your application,
                                 defaults to the value of PYJO_MODE or
                                 "development"
      --threads                  Run the application with the workers of
                                 Pyjo.Server.Prethread
  -v, --verbose                  Print details about what files changed to
                                 sys.stdout
  -w, --watch <directory/file>   One or more directories and files to watch
                                 for changes, defaults to the application
                                 script as well as the "lib" and "templates"
                                 directories in the current working directory

Start Pyjoyment applications with the Pyjo.Server.Reload web server.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::Server::Morbo and script/morbo of Mojolicious 9.49.

Classes

class Pyjo.Server.Reload.Pyjo_Server_Reload(*, backend: Pyjo.Server.Reload.Backend.object | None = None, daemon: Pyjo_Server_Daemon | None = None, silent: bool = True)[source]

Pyjo.Server.Reload inherits all methods from Pyjo.Base and implements the following new ones.

property backend: Pyjo.Server.Reload.Backend.object[source]
backend = reload.backend
reload.backend = Pyjo.Server.Reload.Backend.Poll.new()

Backend, usually a Pyjo.Server.Reload.Backend.Poll object, or Pyjo.Server.Reload.Backend.<Name> of the PYJO_RELOAD_BACKEND environment variable.

property daemon: Pyjo_Server_Daemon[source]
daemon = reload.daemon
reload.daemon = Pyjo.Server.Daemon.new()

Pyjo.Server.Daemon object this server manages.

run(app: str) → NoReturn[source]
reload.run('myapp.py')

Run server for application and wait for the signals.

silent
boolean = reload.silent
reload.silent = False

Disable console messages, defaults to a true value.

Pyjo.Server.Reload.main(argv: Sequence[str] | None = None) → NoReturn[source]
main(['-l', 'http://*:8080', 'myapp.py'])

The command line interface of pyjo-reload: parse the options and run the server for the application.