Last week, Py4J version 0.10.4
was released. Py4J is a bidirectional bridge between python and Java.It enables Python programs running in a Python interpreter to dynamically access Java objects in a JVM.

Py4J

Version 0.10.4 of Py4J allows you to set a value in a Java array that will convert to the Python type, so the value types don’t have to exactly match.

A GatewayServer
instance allows Python programs to communicate with the JVM over a local network socket. It takes an ‘entry point’ object as a parameter. This can be any object, e.g. a list.

Once the GatewayServer
has been started, it can accept incoming Python requests.

On the Python side, a Python interpreter needs to be started (with Py4J in the PYTHONPATH
). Then a JavaGateway
needs to be created, using the same port as the GatewayServer
. From the gateway object, we can then access the entry_point
and start to manipulate it:

Java and Python are fairly stable in their membership of the top 5 programming languages used
. Python is a popular scientific language, and lends itself well to writing machine learning algorithms. In this area, it is easier and quicker to prototype algorithms than Java.

Other options for running Python in Java involve embedding Python code within a Java process, for example Jython
. This dynamically compiles Python to Java bytecodes, which is highly performant. You get the full advantage of Java garbage collection and multithreading. However the Python processes will be using competing with Java processes for resources within the JVM.

Separating the running of the Python process and the Java process sits in well with the trend of moving towards microservices
. It also nicely decouples them. Itsuits a project where you would want to delegate some responsibility to a Python process (for example a machine learning algorithm) that is better suited than the counterpart Java implementation, without impacting on performance.

By default the communication between Python and Java code is handled by one thread per connection on both sides. It does providecallback functionality, and communication can be initiated from the Java or Python side. The benefit of Py4J rather than writing two separate microservices from scratch is that the hard work of writing APIs to convert objects from Java to Python, and vice versa, is done for you.

1.0?

1.0 is planned for 31 December 2016
. In the meantime we can expect performance improvements like a binary transport protocol
. If you are interested in learning more or contributing
, see the Py4J
website.