Server#

class Server(cores=None, version=None, port=None, multi=None, timeout=60, arguments=None)[source]#

Manages a Comsol server process.

Instances of this class start and eventually stop Comsol servers running on the local machine. Clients, either running on the same machine or elsewhere on the network, can then connect to the server at the network port it exposes for that purpose.

Example usage:

import mph
server = mph.Server(cores=1)
print(f'Server listing on port {server.port}.')
server.stop()

The number of processor cores the server makes use of may be restricted. If no number is given, all cores are used by default.

A specific version of the Comsol back-end can be specified if several are installed on the machine, for example version='6.0'. Otherwise the latest version is used.

The server can be instructed to use a specific network port for communication with clients by passing the number of a free port explicitly. If port=None, the default, the server will try to use port 2036 or, in case it is blocked by another server already running, will try subsequent numbers until it finds a free port. This is also Comsol’s default behavior. It is however not robust and may lead to start-up failures if multiple servers are spinning up at the same time. Pass port=0 to work around this issue. The server will then select a random free port, which will almost always avoid collisions.

If multi is False or 'off' or None (the default), then the server will shut down as soon as the first connected clients disconnects itself. If it is True or 'on', the server process will stay alive and accept multiple client connections.

A timeout can be set for the server start-up. The default is 60 seconds. TimeoutError is raised if the server failed to start within that period.

A list of extra command-line arguments can be specified. They are appended to the arguments passed by default when starting the server process, and would thus override them in case of duplicates.

version#

Comsol version (e.g., '6.0') the server is running on.

cores#

Number of processor cores the server was requested to use.

port#

Port number the server is listening on for client connections.

process#

Subprocess that the server is running in.

running()[source]#

Returns whether the server process is still running.

stop(timeout=20)[source]#

Shuts down the server.