Search in sources :

Example 1 with GatewayServer

use of py4j.GatewayServer in project angel by Tencent.

the class PythonGatewayServer method startServer.

private void startServer() throws IOException {
    GatewayServer gatewayServer = new GatewayServer(null, 0);
    gatewayServer.start();
    int boundPort = gatewayServer.getListeningPort();
    if (boundPort == -1) {
        LOG.error("GatewayServer failed to binding");
        System.exit(1);
    } else {
        LOG.debug("GatewayServer started on port: " + boundPort);
    }
    String callbackHost = System.getenv("_PYANGEL_CALLBACK_HOST");
    LOG.info("GatewayServer Host: " + callbackHost);
    int callbackPort = Integer.parseInt(System.getenv("_PYANGEL_CALLBACK_PORT"));
    LOG.info("GatewayServer Port: " + callbackPort);
    try {
        Socket callbackSocket = new Socket(callbackHost, callbackPort);
        DataOutputStream dos = new DataOutputStream(callbackSocket.getOutputStream());
        dos.writeInt(boundPort);
        dos.close();
        callbackSocket.close();
        // Exit on EOF or broken pipe to ensure that this process dies when the Python driver dies:
        while (System.in.read() != -1) {
        // Do nothing
        }
    } catch (IOException ioe) {
        LOG.error("failed to start callback server: ", ioe);
    }
    LOG.debug("Exiting due to broken pipe from Python");
    System.exit(0);
}
Also used : DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) GatewayServer(py4j.GatewayServer) Socket(java.net.Socket)

Example 2 with GatewayServer

use of py4j.GatewayServer in project Anserini by castorini.

the class PyseriniEntryPoint method main.

public static void main(String[] argv) throws Exception {
    System.out.println("starting Gateway Server...");
    GatewayServer gatewayServer = new GatewayServer(new PyseriniEntryPoint());
    gatewayServer.start();
    System.out.println("started!");
}
Also used : GatewayServer(py4j.GatewayServer)

Example 3 with GatewayServer

use of py4j.GatewayServer in project GeoGig by boundlessgeo.

the class SilentProgressListener method main.

public static void main(String[] args) {
    Logging.tryConfigureLogging();
    int port = GatewayServer.DEFAULT_PORT;
    if (args.length != 0) {
        if (args.length > 1) {
            System.out.println("Too many arguments.\n Usage: geogig-gateway [port]");
            return;
        }
        try {
            port = Integer.parseInt(args[0]);
        } catch (NumberFormatException e) {
            System.out.println("Wrong argument: " + args[0] + "\nUsage: geogig-gateway [port]");
            return;
        }
    }
    GatewayServer gatewayServer = new GatewayServer(new GeogigPy4JEntryPoint(), port);
    gatewayServer.start();
    System.out.println("GeoGig server correctly started and waiting for conections at port " + Integer.toString(port));
}
Also used : GatewayServer(py4j.GatewayServer)

Example 4 with GatewayServer

use of py4j.GatewayServer in project cdap by caskdata.

the class AbstractSparkPythonUtil method startPy4jGateway.

/**
 * Starts a Py4j gateway server.
 *
 * @param portFile the file to have the gateway server listening port (in string format) written to
 * @return the gateway server
 * @throws IOException if failed to start the server or failed to write out the port.
 */
public static GatewayServer startPy4jGateway(Path portFile) throws IOException {
    GatewayServer server = new GatewayServer(null, 0);
    try {
        server.start();
    } catch (Py4JNetworkException e) {
        throw new IOException(e);
    }
    // Write the port number in string form to the port file
    Files.write(portFile, Integer.toString(server.getListeningPort()).getBytes(StandardCharsets.UTF_8));
    return server;
}
Also used : Py4JNetworkException(py4j.Py4JNetworkException) IOException(java.io.IOException) GatewayServer(py4j.GatewayServer)

Example 5 with GatewayServer

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

GatewayServer (py4j.GatewayServer)14 IOException (java.io.IOException)5 DataOutputStream (java.io.DataOutputStream)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AngelException (com.tencent.angel.exception.AngelException)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 PipedOutputStream (java.io.PipedOutputStream)1 InetAddress (java.net.InetAddress)1 Socket (java.net.Socket)1 UnknownHostException (java.net.UnknownHostException)1