Skip to content

Commit

Permalink
Add ability to start the python runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
AutonomicPerfectionist committed Sep 14, 2023
1 parent 420ebf6 commit ee4a94b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/main/java/org/myrobotlab/ext/python/PythonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,22 @@ public static int installPipPackages(String python, List<String> packages) {
}

}

public static int runPythonScript(String python, File workingDirectory, String script, String... args) {
try {
return runPythonScriptAsync(python, workingDirectory, script, args).waitFor();
} catch (InterruptedException e) {
throw new SubprocessException("Unable to run script " + script + " with Python command " + python, e);
}
}


public static Process runPythonScriptAsync(String python, File workingDirectory, String script, String... args) {
ProcessBuilder builder = new ProcessBuilder(python, script);
List<String> currCommand = builder.command();
currCommand.addAll(List.of(args));
try {
return builder.inheritIO().directory(workingDirectory).start().waitFor();
} catch (InterruptedException | IOException e) {
return builder.inheritIO().directory(workingDirectory).start();
} catch (IOException e) {
throw new SubprocessException("Unable to run script " + script + " with Python command " + python, e);
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/org/myrobotlab/service/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ public class Runtime extends Service<RuntimeConfig> implements MessageListener,
*/
protected Set<String> startingServices = new HashSet<>();

private static final String PYTHON_VENV_PATH ="python_services/venv";
private static final String PYTHON_SERVICES_PATH = "python_services";

private static final String PYTHON_VENV_PATH = PYTHON_SERVICES_PATH + fs + "venv";

private String pythonCommand;

Expand Down Expand Up @@ -638,6 +640,15 @@ public void setAutoStart(boolean autoStart) throws IOException {
invoke("getStartYml");
}

public void startPythonRuntime() {
PythonUtils.runPythonScriptAsync(
pythonCommand,
new File("."),
PYTHON_SERVICES_PATH + fs + "mrl" + fs + "bootstrap.py",
Platform.getLocalInstance().getId() + "-python"
);
}

/**
* Framework owned method - core of creating a new service. This method will
* create a service with the given name and of the given type. If the type
Expand Down
11 changes: 11 additions & 0 deletions src/main/python/python_services/mrl/bootstrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import logging
import sys

from mrlpy import mcommand
from mrlpy.framework import runtime
from mrlpy.framework.runtime import Runtime
runtime.runtime_id = sys.argv[1]

Runtime.init_runtime()
logging.basicConfig(level=logging.INFO, force=True)
mcommand.connect(id=sys.argv[1], daemon=False)

0 comments on commit ee4a94b

Please sign in to comment.