service

Subpackage start braynsService as a subprocess from Python.

Provides mainly helpers to generate the command line.

service

class LogLevel(value)

Bases: Enum

Server (braynsService backend) log level.

CRITICAL = 'critical'
DEBUG = 'debug'
ERROR = 'error'
INFO = 'info'
OFF = 'off'
TRACE = 'trace'
WARN = 'warn'
class Service(uri: str, ssl_context: SslServerContext | None = None, max_clients: int = 1, log_level: LogLevel = LogLevel.WARN, executable: str = 'braynsService', env: dict[str, str] = <factory>)

Bases: object

Class used to start a braynsService subprocess.

Use a braynsService executable to start a subprocess. By default it looks for a ‘braynsService’ binary in the PATH but it can be changed.

URI is the websocket server URI (ip:port). Use 0.0.0.0 as wildcard to allow connections from any machine.

SSL server settings can be specified using optional certificate, key, CA and password.

The backend log level can also be specified using log_level.

Custom environment variables can also be set for the subprocess, for example to override the PATH and load specific libraries.

Parameters:
  • uri (str) – Service URI with format ‘host:port’.

  • ssl_context (SslServerContext | None, optional) – SSL context if secure, defaults to None.

  • max_clients (int, optional) – Max simultaneous connections authorized, defaults to 1.

  • log_level (LogLevel, optional) – Process log level, defaults to LogLevel.WARN.

  • executable (str, optional) – braynsService executable, defaults to ‘braynService’.

  • env (dict[str, str], optional) – Subprocess environment variables, default to empty.

env: dict[str, str]
executable: str = 'braynsService'
get_command_line() list[str]

Build the command line to start braynsService.

Returns:

Command line arguments.

Return type:

list[str]

log_level: LogLevel = 'warn'
max_clients: int = 1
ssl_context: SslServerContext | None = None
start() Process

Start a new process for a braynsService backend.

Return the process which runs the service.

Returns:

Service process.

Return type:

Process

uri: str
class SslServerContext(private_key_file: str | None = None, private_key_passphrase: str | None = None, certificate_file: str | None = None, ca_location: str | None = None)

Bases: object

Server SSL context.

Can be used to start a braynsService instance with SSL enabled. Optional parameters such as the server private key, certificate and trusted CAs can be specified here.

ca_location: str | None = None
certificate_file: str | None = None
private_key_file: str | None = None
private_key_passphrase: str | None = None

manager

class Manager(process: Process, instance: Instance)

Bases: NamedTuple

Wrapper to manage a Process connected to an Instance.

Contains a process running braynService and an instance connected to it.

Once done with this object, stop() must be called to disconnect the instance and kill the process, using the context manager or by hand.

Parameters:
  • process (Process) – Process running braynsService.

  • instance (Instance) – Instance connected to process.

Create new instance of Manager(process, instance)

instance: Instance

Alias for field number 1

process: Process

Alias for field number 0

stop() None

Disconnect the instance and kill the process.

start(service: Service, connector: Connector) Manager

Start a braynsService instance and connect to it.

Return the service process and the connected instance (see Manager).

Parameters:
  • service (Service) – Service specifications.

  • connector (Connector) – Connection specifications.

Returns:

Running process and connected instance.

Return type:

Manager

process

class Process(args: list[str], env: dict[str, str])

Bases: object

Process running a braynsService backend.

The process must be stopped using terminate() once done with it, otherwise it will run forever. Use context manager (ie with process: …) to avoid mistakes.

The last lines of the process logs can be retreived using logs.

property alive: bool

Check if the process is still running.

property logs: str

Get the last logs (stdout + stderr) from the process.

stop() None

Stop the backend service by terminating the process.

Must be called through the context manager (i.e. ‘with’) or manually to avoid the process to run forever.