use of py4j.Py4JPythonClient in project flink by apache.
the class PythonGatewayServer method main.
/**
* Main method to start a local GatewayServer on a ephemeral port. It tells python side via a
* file.
*
* <p>See: py4j.GatewayServer.main()
*/
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
GatewayServer gatewayServer = PythonEnvUtils.startGatewayServer();
PythonEnvUtils.setGatewayServer(gatewayServer);
int boundPort = gatewayServer.getListeningPort();
Py4JPythonClient callbackClient = gatewayServer.getCallbackClient();
int callbackPort = callbackClient.getPort();
if (boundPort == -1) {
System.out.println("GatewayServer failed to bind; exiting");
System.exit(1);
}
// Tells python side the port of our java rpc server
String handshakeFilePath = System.getenv("_PYFLINK_CONN_INFO_PATH");
File handshakeFile = new File(handshakeFilePath);
File tmpPath = Files.createTempFile(handshakeFile.getParentFile().toPath(), "connection", ".info").toFile();
FileOutputStream fileOutputStream = new FileOutputStream(tmpPath);
DataOutputStream stream = new DataOutputStream(fileOutputStream);
stream.writeInt(boundPort);
stream.writeInt(callbackPort);
stream.close();
fileOutputStream.close();
if (!tmpPath.renameTo(handshakeFile)) {
System.out.println("Unable to write connection information to handshake file: " + handshakeFilePath + ", now exit...");
System.exit(1);
}
try {
// This ensures that the server dies if its parent program dies.
Map<String, Object> entryPoint = (Map<String, Object>) gatewayServer.getGateway().getEntryPoint();
for (int i = 0; i < TIMEOUT_MILLIS / CHECK_INTERVAL; i++) {
if (entryPoint.containsKey("Watchdog")) {
break;
}
Thread.sleep(CHECK_INTERVAL);
}
if (!entryPoint.containsKey("Watchdog")) {
System.out.println("Unable to get the Python watchdog object, now exit.");
System.exit(1);
}
Watchdog watchdog = (Watchdog) entryPoint.get("Watchdog");
while (watchdog.ping()) {
Thread.sleep(CHECK_INTERVAL);
}
gatewayServer.shutdown();
System.exit(0);
} finally {
System.exit(1);
}
}
Aggregations