linopy.remote.RemoteHandler#
- class linopy.remote.RemoteHandler(hostname, port=22, username=None, password=None, client=None, python_script=<built-in method format of str object>, python_executable='python', python_file='/tmp/linopy-execution.py', model_unsolved_file='/tmp/linopy-unsolved-model.nc', model_solved_file='/tmp/linopy-solved-model.nc')#
Handler class for solving models on a remote machine via an SSH connection.
The basic idea of the handler is to provide a workflow that:
defines a model on the local machine
saves it to a file on the local machine
copies that file to the remote machine
loads, solves and writes out the model, all on the remote machine
copies the solved model to the local machine
loads the solved model on the local machine
The Handler opens an interactive shell in which the commands are executed. All standard outputs of the remote are directly displayed in the local prompt. You can directly set a connected SSH client for the RemoteHandler if you don’t want to use the default connection parameters host, username and password.
If the SSH keys are stored in a default location, the keys are autodetected and the RemoteHandler does not require a password argument.
- Parameters:
hostname (
str) – Name of the server to connect to. This is used if client is None.port (
int) – The server port to connect to. This is used if client is None.username (
str) – The username to authenticate as (defaults to the current local username). This is used if client is None.password (
str) – Used for password authentication; is also used for private key decryption. Not necessary if ssh keys are auto-detectable. This is used if client is None.client (
paramiko.SSHClient) – Already connected client to use instead of initializing a one with the above arguments.python_script (
callable) – Format function which takes the arguments model_unsolved_file, solve_kwargs and model_solved_files. Defaults to linopy.remote.command.format, where linopy.remote.command is the string of the python command.python_executable (
str) – Python executable to use on the remote machine.python_file (
str) – Path where to store the python script on the remote machine.model_unsolved_file (
str) – Path where to temporarily store the unsolved model on the local machine before copying it over.model_solved_file (
str) – Path where to temporarily store the solved model on the remote machine.
Example
>>> import linopy >>> from linopy import Model >>> from numpy import arange >>> from xarray import DataArray >>> >>> N = 10 >>> m = Model() >>> coords = [arange(N), arange(N)] >>> x = m.add_variables(coords=coords) >>> y = m.add_variables(coords=coords) >>> con1 = m.add_constraints(x - y >= DataArray(arange(N))) >>> con2 = m.add_constraints(x + y >= 0) >>> obj = m.add_objective((2 * x + y).sum()) >>> >>> host = "my-remote-machine.com" >>> username = "my-username" >>> handler = linopy.remote.RemoteHandler(host, username=username) >>> >>> # optionally activate a conda environment >>> handler.execute("conda activate my-linopy-env") >>> >>> m = handler.solve_on_remote(m)
- __init__(hostname, port=22, username=None, password=None, client=None, python_script=<built-in method format of str object>, python_executable='python', python_file='/tmp/linopy-execution.py', model_unsolved_file='/tmp/linopy-unsolved-model.nc', model_solved_file='/tmp/linopy-solved-model.nc')#
Methods
__init__(hostname[, port, username, ...])execute(cmd)Execute a shell command on the remote machine.
python_scriptsolve_on_remote(model, *[, reformulate_sos])Solve a linopy model on the remote machine.
write_model_on_remote(model)Write a model on the remote machine under self.model_unsolved_file.
write_python_file_on_remote(**solve_kwargs)Write the python file of the RemoteHandler on the remote machine under self.python_file.
Attributes
clientmodel_solved_filemodel_unsolved_filepasswordportpython_executablepython_fileusernamehostname