未加星标

Py4J: Building bridges between Python and Java

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

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.

The Java programmight 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. 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. 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.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: JavaJVMPython
分页:12
转载请注明
本文标题:Py4J: Building bridges between Python and Java
本站链接:http://www.codesec.net/view/484445.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(30)