Daemon Configuration

Daemon Configuration#

While servers, often referred to as daemons in the PyroLab docs, and services registered through them can be thought about and developed separately, in deployment, their configurations are linked. Hence this section will again reference services, in addition to servers.

Server daemons and service configurations are only important for host machines providing services to a PyroLab network. For a client simply connecting to the network to utilize available PyroLab services, there is no need to configure servers or services.

Let’s take a look at the server configuration class, DaemonConfiguration.


class DaemonConfiguration(_env_file: DotenvType | None = '<object object>', _env_file_encoding: str | None = None, _env_nested_delimiter: str | None = None, _secrets_dir: StrPath | None = None, *, module: str = 'pyrolab.server', classname: str = 'Daemon', host: str = 'localhost', port: int = 0, unixsocket: str | None = None, nathost: str | None = None, natport: int = 0, servertype: str = 'thread', nameservers: List[str] = [])[source]

Bases: BaseSettings, PyroConfigMixin, YAMLMixin

Server configuration object.

Note that for the host parameter, the string “public” will always be reevaluated to the computer’s public IP address.

Parameters:
modulestr, optional

The module that contains the Daemon class (default “pyrolab.server”).

classnamestr, optional

The name of the Daemon class to use (default is basic “Daemon”).

hoststr, optional

The hostname of the local server, or the string “public”, which is converted to the host’s public IP address (default “localhost”).

portint, optional

Port to bind the server on (default 0, which means to pick a random port).

unixsocketstr, optional

The name of a Unix domain socket to use instead of a TCP/IP socket (default None, which means don’t use).

nathoststr, optional

Hostname to use in published addresses (useful when running behind a NAT firewall/router). Default is None which means to just use the normal host. For more details about NAT, see the Pyro5 docs about using Pyro5 behind a NAT router/firewall.

natportint, optional

Port to use in published addresses (useful when running behind a NAT firewall/router). If you use 0 here (default), Pyro will replace the NAT-port by the internal port number to facilitate one-to-one NAT port mappings.

servertypestr, optional

Either thread or multiplex (default “thread”).

nameserversList[str], optional

Whether to register the daemon itself with known nameservers. Useful if the daemon provides functions for managing local instruments that would be useful to remote clients. Services declare their own nameserver registrations; belonging to a daemon that registers itself with a specific nameserver does not mean that its services will also be registered with that nameserver.

Examples

The following is an example of a valid configuration file “daemons” section. Keys not defined assume the default values.

daemons:
    lockable:
        classname: LockableDaemon
        host: public
        servertype: thread
        nameservers:
            - production
    multiplexed:
        host: public
        servertype: multiplex
__init__(_env_file: str | PathLike | List[str | PathLike] | Tuple[str | PathLike, ...] | None = '<object object>', _env_file_encoding: str | None = None, _env_nested_delimiter: str | None = None, _secrets_dir: str | PathLike | None = None, **values: Any) None

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.


First, notice that all the configuration values have “sensible” defaults. PyroLab by default keeps everything on localhost for security’s sake—you don’t want to unwittingly open up your ports to the world. You have to explicitly tell PyroLab to do that.

Second, notice that in a YAML representation of the nameserver’s configuration, only the keys that are different from the defaults need to be defined; the model that loads the file will automatically populate the missing fields with the default values.

We can work with DaemonConfiguration objects either through a text YAML file, or by constructing the actual Python object. For now, we’ll use Python directly:

Listing 1 The default server configuration, interacting directly with the Python object.#
>>> from pyrolab.configure import DaemonConfiguration
>>> config = DaemonConfiguration()
>>> config.module, config.classname, config.host, config.port
('pyrolab.daemon', 'Daemon', 'localhost', 0)
Listing 2 Server configuration, interacting with the Python object, with values modified from the default.#
>>> from pyrolab.configure import DaemonConfiguration
>>> config = DaemonConfiguration(classname="LockableDaemon", host="public", port=2022)
>>> config.module, config.classname, config.host, config.port
('pyrolab.daemon', 'LockableDaemon', 'public', 2022)

These objects have a yaml() method that can be used to dump the configuration to a YAML file. It accepts a parameter that allows you to dump only the values that differ from the default:

Listing 3 The default server configuration, interacting with the YAML representation.#
>>> from pyrolab.configure import DaemonConfiguration
>>> config = DaemonConfiguration()
>>> print(config.yaml())
module: pyrolab.server
classname: Daemon
host: localhost
port: 0
unixsocket: null
nathost: null
natport: 0
servertype: thread
Listing 4 Modified server configuration, interacting with the YAML representation, with defaults excluded.#
>>> from pyrolab.configure import DaemonConfiguration
>>> config = DaemonConfiguration(classname="LockableDaemon", host="public", port=2022)
>>> print(config.yaml(exclude_defaults=True))
classname: LockableDaemon
host: public
port: 2022