Search in sources :

Example 16 with GatewayServer

use of py4j.GatewayServer in project zeppelin by apache.

the class PythonUtils method createGatewayServer.

public static GatewayServer createGatewayServer(Object entryPoint, String serverAddress, int port, String secretKey, boolean useAuth) throws IOException {
    LOGGER.info("Launching GatewayServer at " + serverAddress + ":" + port + ", useAuth: " + useAuth);
    if (useAuth) {
        try {
            Class clz = Class.forName("py4j.GatewayServer$GatewayServerBuilder", true, Thread.currentThread().getContextClassLoader());
            Object builder = clz.getConstructor(Object.class).newInstance(entryPoint);
            builder.getClass().getMethod("authToken", String.class).invoke(builder, secretKey);
            builder.getClass().getMethod("javaPort", int.class).invoke(builder, port);
            builder.getClass().getMethod("javaAddress", InetAddress.class).invoke(builder, InetAddress.getByName(serverAddress));
            builder.getClass().getMethod("callbackClient", int.class, InetAddress.class, String.class).invoke(builder, port, InetAddress.getByName(serverAddress), secretKey);
            return (GatewayServer) builder.getClass().getMethod("build").invoke(builder);
        } catch (Exception e) {
            throw new IOException(e);
        }
    } else {
        return new GatewayServer(entryPoint, port, GatewayServer.DEFAULT_PYTHON_PORT, InetAddress.getByName(serverAddress), InetAddress.getByName(serverAddress), GatewayServer.DEFAULT_CONNECT_TIMEOUT, GatewayServer.DEFAULT_READ_TIMEOUT, (List) null);
    }
}
Also used : IOException(java.io.IOException) GatewayServer(py4j.GatewayServer) InetAddress(java.net.InetAddress) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 17 with GatewayServer

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

GatewayServer (py4j.GatewayServer)17 IOException (java.io.IOException)7 File (java.io.File)4 DataOutputStream (java.io.DataOutputStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 DefaultGatewayServerListener (py4j.DefaultGatewayServerListener)2 AngelException (com.tencent.angel.exception.AngelException)1 JobConfig (edu.iu.dsc.tws.api.JobConfig)1 Twister2Job (edu.iu.dsc.tws.api.Twister2Job)1 Config (edu.iu.dsc.tws.api.config.Config)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PipedInputStream (java.io.PipedInputStream)1