两种方式,java< => python,使用py4j进行通信

时间:2017-05-24 05:42:43

标签: java python py4j

我使用py4j进行python和java之间的通信。我可以从java端调用python方法。但是从python我无法发送任何对象或调用java方法。这是我试过的代码。

我的java代码:

public interface IHello {
    public String sayHello();

    public String sayHello(int i, String s);

//    public String frompython();

}

//ExampleClientApplication.java
package py4j.examples;

import py4j.GatewayServer;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExampleClientApplication extends Thread {

public void run(){
    System.out.println("thread is running...");
}

public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    GatewayServer.turnLoggingOff();
    GatewayServer server = new GatewayServer();
    server.start();
    IHello hello = (IHello) server.getPythonServerEntryPoint(new Class[] { IHello.class });
    try {
        System.out.println("Please enter a string");
        String str = br.readLine();
        System.out.println(hello.sayHello(1, str));
    } catch (Exception e) {
        e.printStackTrace();
    }
    ExampleClientApplication t1 = new ExampleClientApplication();
    t1.start();
    //server.shutdown();
  }
}

我的python代码:

class SimpleHello(object):
    def sayHello(self, int_value=None, string_value=None):
        print(int_value, string_value)
        return "From python to {0}".format(string_value)

class Java:
    implements = ["py4j.examples.IHello"]

# Make sure that the python code is started first.
# Then execute: java -cp py4j.jar 
py4j.examples.SingleThreadClientApplication

from py4j.java_gateway import JavaGateway, CallbackServerParameters
simple_hello = SimpleHello()

gateway = JavaGateway(
callback_server_parameters=CallbackServerParameters(),
python_server_entry_point=simple_hello)

1 个答案:

答案 0 :(得分:0)

通过在python实现的java接口中实现getter / eval方法可以轻松解决发送对象问题,然后可以从java调用该方法来获取所请求的变量。有关示例,请查看此处的py4j实现:https://github.com/subes/invesdwin-context-python

具体请参见此处的get / eval方法实现:https://github.com/subes/invesdwin-context-python/blob/master/invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/de/invesdwin/context/python/runtime/py4j/pool/internal/Py4jInterpreter.py

反过来说,你必须在java网关类上做同样的事情,并提供一个可以从python调用的get / eval方法。实际的java代码可以在ScriptEngine中执行以获得groovy,结果可以返回给python。 (见http://docs.groovy-lang.org/1.8.9/html/groovy-jdk/javax/script/ScriptEngine.html

虽然决定一个主/从角色并且只输入一次参数可能是一个更好的主意,然后在python中执行代码并检索一次结果。或者反过来说如果python应该领先。因此,您可以大大减少通信开销。为了实现更紧密的集成而没有py4j引起的性能损失,你应该看看https://github.com/mrj0/jep直接将python库加载到java进程中。然后每次通话都不再昂贵了。