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.

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.

The Java program might look like this:

package py4j.examples;

import py4j.GatewayServer;

public class StackEntryPoint {

    private Stack stack;

    public StackEntryPoint() {
      stack = new Stack();
      stack.push("Initial Item");
    }

    public Stack getStack() {
        return stack;
    }

    public static void main(String[] args) {
        //Here, the 'entry point' object is StackEntryPoint, 
        // which creates a new Stack object. This wraps around a 
        // List and has pop() and push(String element) 
        // methods to manipulate the List
        GatewayServer gatewayServer = new GatewayServer(new StackEntryPoint());
        gatewayServer.start();
    }

}

Example modified from the Py4J Getting Started guide.

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:

>>> from py4j.java_gateway import JavaGateway
>>> gateway = JavaGateway()
>>> stack = gateway.entry_point.getStack()
>>> stack.push("First %s" % ('item'))
>>> stack.push("Second item")
>>> stack.pop()
u'Second item'

For more examples, see the Getting Started guide.

Not on my JVM

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. It suits 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 provide callback 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.

Py4J: Building bridges between Python and Java

| Programming Languages| 2,981 views | 0 Comments
About The Author
- Katharine is a Java developer by trade, turned Community & Content Manager for Voxxed. Helping developers learn and share knowledge. Contact me at kbe@voxxed.com with any news, articles or tutorials.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>