Search in sources :

Example 1 with Gateway

use of py4j.Gateway in project flink by apache.

the class PythonEnvUtils method startGatewayServer.

/**
 * Creates a GatewayServer run in a daemon thread.
 *
 * @return The created GatewayServer
 */
static GatewayServer startGatewayServer() throws ExecutionException, InterruptedException {
    CompletableFuture<GatewayServer> gatewayServerFuture = new CompletableFuture<>();
    Thread thread = new Thread(() -> {
        try (NetUtils.Port port = NetUtils.getAvailablePort()) {
            int freePort = port.getPort();
            GatewayServer server = new GatewayServer.GatewayServerBuilder().gateway(new Gateway(new ConcurrentHashMap<String, Object>(), new CallbackClient(freePort))).javaPort(0).build();
            resetCallbackClientExecutorService(server);
            gatewayServerFuture.complete(server);
            server.start(true);
        } catch (Throwable e) {
            gatewayServerFuture.completeExceptionally(e);
        }
    });
    thread.setName("py4j-gateway");
    thread.setDaemon(true);
    thread.start();
    thread.join();
    return gatewayServerFuture.get();
}
Also used : CallbackClient(py4j.CallbackClient) CompletableFuture(java.util.concurrent.CompletableFuture) NetUtils(org.apache.flink.util.NetUtils) Gateway(py4j.Gateway) GatewayServer(py4j.GatewayServer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 NetUtils (org.apache.flink.util.NetUtils)1 CallbackClient (py4j.CallbackClient)1 Gateway (py4j.Gateway)1 GatewayServer (py4j.GatewayServer)1