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')
reload.run('/home/sri/myapp.py', 'prefork', '-w', '1')

Pyjo.Server.Reload.new(exec=['npx webpack --watch']).run('/home/sri/myapp.py')

Pyjo.Server.Reload is a self-restart capable development server. It starts your application with a server command, like Pyjoyment.Command.daemon or Pyjoyment.Command.prefork, and restarts it whenever a file changes. Listen sockets are only created once and then passed on to the server, so they should be configured with Pyjo.Server.Reload and not the server command. Note that the server uses signals for process management, so you should avoid modifying signal handlers in your applications.

Additional commands, like asset bundlers in watch mode, can be started alongside the server with exec. They only get started once, are not restarted together with the server, and get stopped when Pyjo.Server.Reload shuts down.

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

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

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

The server and the commands run in process groups of their own, which the signals of POSIX stop, so it does not run 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. The server, all exec commands and their subprocesses get a TERM signal, and the server also QUIT, and will be killed if they have not stopped after 5 seconds, just like for restarts.

HUP

Restart the server, as a change of a file does, while the exec commands go on.

pyjo-reload

Usage: pyjo-reload [OPTIONS] APPLICATION [COMMAND] [ARGS]

  pyjo-reload ./myapp.py
  pyjo-reload ./myapp.py prefork -w 1
  pyjo-reload -l http://127.0.0.1:8080 -w assets/css ./myapp.py prefork -w 3
  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
  pyjo-reload -x 'npx webpack --watch' ./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", "plain"
                                 without time and process, 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"
  -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
  -x, --exec <command>           One or more shell commands to start
                                 alongside the server, like asset bundlers in
                                 watch mode

Start Pyjoyment applications with the Pyjo.Server.Reload restarter. The server command defaults to daemon, and all options of pyjo-reload need to come before the application. Shell commands started with --exec run for as long as Pyjo.Server.Reload does, they are not restarted together with the server.

See also

Pyjoyment, https://mojolicious.org.

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

Classes

class Pyjo.Server.Reload.Pyjo_Server_Reload(*, backend: Pyjo.Server.Reload.Backend.object | None = None, daemon: Pyjo_Server_Daemon | None = None, exec: list[str] | 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 used to create listen sockets. Other settings will be ignored, since the server runs in a separate process, use options of the server command instead.

exec: list[str]
commands = reload.exec
reload.exec = ['npx webpack --watch', 'npx tailwindcss -i app.css -o public/assets/app.css --watch']

Shell commands to start alongside the server, like asset bundlers in watch mode. The PYJO_MODE environment variable will be set to the operating mode of your application, so commands can use it too. The standard input of all commands is a pipe that gets closed when Pyjo.Server.Reload exits, even if it gets killed unexpectedly, which some tools use to shut down. When a command exits on its own, a message with its exit status is written to sys.stderr.

run(app: str, *args: str) → NoReturn[source]
reload.run('myapp.py')
reload.run('myapp.py', 'prefork', '-w', '1')

Run server command for application and wait for the signals. The command defaults to daemon.

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'])
main(['-l', 'http://*:8080', 'myapp.py', 'prefork', '-w', '2'])

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