Search in sources :

Example 1 with Py4JPythonClient

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);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) Py4JPythonClient(py4j.Py4JPythonClient) GatewayServer(py4j.GatewayServer) File(java.io.File) Map(java.util.Map)

Aggregations

DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Map (java.util.Map)1 GatewayServer (py4j.GatewayServer)1 Py4JPythonClient (py4j.Py4JPythonClient)1