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);
}
}
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;
}
Aggregations