Search in sources :

Example 1 with Py4JServerConnection

use of py4j.Py4JServerConnection in project twister2 by DSC-SPIDAL.

the class PythonWorker method initJavaServer.

private static GatewayServer initJavaServer(int port, EntryPoint entryPoint, String pythonPath, boolean bootstrap, String[] args, boolean useMPIIfAvailable, int worldSize, Config config) {
    GatewayServer py4jServer = new GatewayServer(entryPoint, port);
    py4jServer.addListener(new DefaultGatewayServerListener() {

        @Override
        public void connectionStarted(Py4JServerConnection gatewayConnection) {
            LOG.info("Connection established");
        }

        @Override
        public void connectionStopped(Py4JServerConnection gatewayConnection) {
            // this will release all the threads who are waiting on waitForCompletion()
            entryPoint.close();
        }

        @Override
        public void serverStarted() {
            LOG.info("Started java server on " + port);
            new Thread(() -> {
                try {
                    startPythonProcess(pythonPath, port, bootstrap, args, useMPIIfAvailable, worldSize, config);
                    entryPoint.waitForCompletion();
                    py4jServer.shutdown();
                } catch (IOException e) {
                    LOG.log(Level.SEVERE, "Error in starting python process");
                } catch (MPIException e) {
                    LOG.log(Level.SEVERE, "Python process failed to start with MPI Support", e);
                } catch (InterruptedException e) {
                    LOG.log(Level.SEVERE, "Failed while waiting for the completion of python process", e);
                }
            }, "python-process" + (bootstrap ? "-bootstrap" : "")).start();
        }
    });
    return py4jServer;
}
Also used : Py4JServerConnection(py4j.Py4JServerConnection) MPIException(mpi.MPIException) DefaultGatewayServerListener(py4j.DefaultGatewayServerListener) IOException(java.io.IOException) GatewayServer(py4j.GatewayServer)

Aggregations

IOException (java.io.IOException)1 MPIException (mpi.MPIException)1 DefaultGatewayServerListener (py4j.DefaultGatewayServerListener)1 GatewayServer (py4j.GatewayServer)1 Py4JServerConnection (py4j.Py4JServerConnection)1