Skip to content

Instantly share code, notes, and snippets.

@pozdneev
Forked from bartdag/README.md
Created June 4, 2020 16:40
Show Gist options
  • Save pozdneev/60678b1db8835d60fcc2c3430ff4722d to your computer and use it in GitHub Desktop.
Save pozdneev/60678b1db8835d60fcc2c3430ff4722d to your computer and use it in GitHub Desktop.
Py4J - Multiple GatewayServer and CallbackServer instances

Py4J Multiple GatewayServer and CallbackServer instances

Compile and run TestApplication.java with py4j.jar in your classpath.

Then execute python3 test.py

Explanations

This creates three pairs of GatewayServer and CallbackServer with different ports on both the Java and Python sides. Python is driving the communication by asking Java to print a String. Then it calls a method that calls back a Python instance. Finally, it shuts down the Java GatewayServer. When the three GatewayServer instances are shut down, the Java and the Python programs exit.

from functools import partial
from threading import Thread
from py4j.java_gateway import (
JavaGateway, GatewayParameters, CallbackServerParameters)
class IHelloImpl(object):
def sayHello(self, i=None, s=None):
if i is None:
return "This is Hello!"
else:
return "This is Hello;\n{0}{1}".format(i, s)
class Java:
implements = ["py4j.examples.IHello"]
def build_gateway(java_port, python_port):
gateway_parameters = GatewayParameters(port=java_port)
callback_server_parameters = CallbackServerParameters(
port=python_port)
gateway = JavaGateway(
gateway_parameters=gateway_parameters,
callback_server_parameters=callback_server_parameters)
return gateway
def execute(gateway, name):
gateway.jvm.System.out.println("Hello from {0}".format(name))
example = gateway.jvm.py4j.examples.ExampleClass()
hello = IHelloImpl()
output = example.callHello(hello);
print(output)
gateway.shutdown();
def main():
gateway1 = build_gateway(10001, 10002)
gateway2 = build_gateway(10003, 10004)
gateway3 = build_gateway(10005, 10006)
execute1 = partial(execute, gateway1, "gateway1")
execute2 = partial(execute, gateway2, "gateway2")
execute3 = partial(execute, gateway3, "gateway3")
t1 = Thread(target=execute1)
t2 = Thread(target=execute2)
t3 = Thread(target=execute3)
t1.start()
t2.start()
t3.start()
if __name__ == "__main__":
main()
package py4j;
import java.net.InetAddress;
public class TestApplication {
public static void main(String[] args) {
try {
GatewayServer server1 = new GatewayServer.GatewayServerBuilder()
.javaPort(10001)
.javaAddress(InetAddress.getByName("127.0.0.1"))
.callbackClient(10002, InetAddress.getByName("127.0.0.1"))
.build();
GatewayServer server2 = new GatewayServer.GatewayServerBuilder()
.javaPort(10003)
.javaAddress(InetAddress.getByName("127.0.0.1"))
.callbackClient(10004, InetAddress.getByName("127.0.0.1"))
.build();
GatewayServer server3 = new GatewayServer.GatewayServerBuilder()
.javaPort(10005)
.javaAddress(InetAddress.getByName("127.0.0.1"))
.callbackClient(10006, InetAddress.getByName("127.0.0.1"))
.build();
server1.start();
server2.start();
server3.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment