Search in sources :

Example 1 with DefaultGatewayServerListener

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

the class PythonWorker method execute.

public void execute(BatchEnvironment env) {
    int port = env.getConfig().getIntegerValue(PYTHON_PORT_OFFSET) + env.getWorkerID();
    Twister2Environment twister2Environment = new Twister2Environment(env);
    String tw2Home = env.getConfig().getStringValue("twister2.directory.home");
    String pythonFileName = env.getConfig().getStringValue("python_file");
    String[] args = (String[]) env.getConfig().get("args");
    String mainPy = new File(tw2Home, pythonFileName).getAbsolutePath();
    boolean mpiAwarePython = env.getConfig().getBooleanValue(MPI_AWARE_PYTHON, false);
    if (mpiAwarePython) {
        LOG.info("Python has requested MPI awareness");
    }
    GatewayServer gatewayServer = initJavaServer(port, twister2Environment, mainPy, false, args, mpiAwarePython, env.getNoOfWorkers(), env.getConfig());
    gatewayServer.start();
    final Semaphore wait = new Semaphore(0);
    gatewayServer.addListener(new DefaultGatewayServerListener() {

        @Override
        public void serverStopped() {
            wait.release();
        }
    });
    try {
        wait.acquire();
    } catch (InterruptedException e) {
        LOG.log(Level.SEVERE, "Failed while waiting for the server to stop", e);
    }
}
Also used : DefaultGatewayServerListener(py4j.DefaultGatewayServerListener) Semaphore(java.util.concurrent.Semaphore) GatewayServer(py4j.GatewayServer) File(java.io.File)

Example 2 with DefaultGatewayServerListener

use of py4j.DefaultGatewayServerListener 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

DefaultGatewayServerListener (py4j.DefaultGatewayServerListener)2 GatewayServer (py4j.GatewayServer)2 File (java.io.File)1 IOException (java.io.IOException)1 Semaphore (java.util.concurrent.Semaphore)1 MPIException (mpi.MPIException)1 Py4JServerConnection (py4j.Py4JServerConnection)1