Search in sources :

Example 1 with LogWriter

use of org.csstudio.display.builder.runtime.LogWriter in project org.csstudio.display.builder by kasemir.

the class PythonGatewaySupport method run.

/**
 * Run a Python script, with a map of Java objects made accessible through a
 * gateway to Python. The port to which the gateway server is listening is
 * passed as an argument to the script.
 *
 * @param map Map which is to be accessed by the script
 * @param script Path (including name) of script which is to be run
 * @throws Exception If unable to execute command to run script
 */
public static void run(Map<String, Object> map, String script) throws Exception {
    GatewayServer server = new GatewayServer(new MapWrapper(map), 0);
    server.start();
    int port = server.getListeningPort();
    if (port == -1) {
        server.shutdown();
        throw new Exception("Exception instantiating PythonGatewaySupport: GatewayServer not listening");
    }
    // start Python process, passing port used to connect to Py4J Java Gateway
    final Process process = new ProcessBuilder("python", script, Integer.toString(port)).start();
    final Thread error_log = new LogWriter(process.getErrorStream(), "PythonErrors", Level.WARNING);
    final Thread python_out = new LogWriter(process.getInputStream(), "PythonOutput", Level.INFO);
    error_log.start();
    python_out.start();
    try {
        process.waitFor();
    } catch (InterruptedException ex) {
    // ignore; closing display creates interruption
    } finally {
        process.destroyForcibly();
    }
    error_log.join();
    python_out.join();
    server.shutdown();
}
Also used : LogWriter(org.csstudio.display.builder.runtime.LogWriter) GatewayServer(py4j.GatewayServer)

Aggregations

LogWriter (org.csstudio.display.builder.runtime.LogWriter)1 GatewayServer (py4j.GatewayServer)1