Is it possible to call Java methods on a different server from Python using Py4J

Viewed 126

I am using py4j for communication between python and java.
Currently, the java processes and python are in same server.
I would like to know is it possible to separate these two into different servers without much changes in the current approach using Py4J.
If possible, can someone help me direct to relevant documentation or show code snippets on how to do this?

1 Answers

I don't know you are still looking for answer but someone might find this helpful :

When you are creating your gateway by calling constructor,

GatewayServer gatewayServer = new GatewayServer(new YourEntryPoint(), gatewayPort);

then your gateway will have default java IP address set to 127.0.0.1 (localhost). Gateway which has IP address set to localhost will answer only requests that come from localhost (same machine).

You can use IP 0.0.0.0 to make it use all IPs of your machine or specify one of IPs of your machine.

One way to specify Java IP is using GatewayServer.GatewayServerBuilder.

GatewayServer.GatewayServerBuilder builder = new GatewayServer.GatewayServerBuilder();
GatewayServer gatewayServer = builder
       .entryPoint(new YourEntryPoint())
       .javaPort(gatewayPort)
       .javaAddress(InetAddress.getByName("0.0.0.0"))  // .getByName throws UnknownHostException
       .build();
Related