首页 > 编程语言 > 详细

py4f通过ClientServer使用单线程模式请求python服务

时间:2020-01-13 18:35:39      阅读:132      评论:0      收藏:0      [点我收藏+]

在py4j的官方文档https://www.py4j.org/advanced_topics.html#initiating-the-communication-from-the-java-side

3.4.2提到使用ClientServer以单线程模式请求python服务。但是范例中只给出了python代码,java代码经测试报错。配套的java代码修改如下:

public class PingPlayer {

    public String firstPing(PongPlayer player) {
        System.out.println("first ping!");
        return player.firstPong(this);
    }

    public String secondPing(PongPlayer player) {
        System.out.println("secondPing");
        return player.secondPong(this);
    }

    public static void main(String[] args) {
        PingPlayer application = new PingPlayer();
        ClientServer clientServer = new ClientServer (application);
        clientServer.startServer();
        System.out.println("PingPong Gateway Server Started");
    }
}

这里对应的java代码中也需要使用ClientServer。才能成功调用。否则会报如下错误:

py4j.protocol.Py4JJavaError: An error occurred while calling o0.firstPing.
: py4j.Py4JException: Command Part is Empty or is the End of Command Part
    at py4j.Protocol.getObject(Protocol.java:283)
    at py4j.Protocol.getReturnValue(Protocol.java:471)
    at py4j.reflection.PythonProxyHandler.invoke(PythonProxyHandler.java:108)
    at com.sun.proxy.$Proxy0.firstPong(Unknown Source)
    at com.huatai.py4jtest3.PingPlayer.firstPing(PingPlayer.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

ERROR:root:Exception while sending command.
Traceback (most recent call last):
  File "D:\Program Files (x86)\Python27\lib\site-packages\py4j\java_gateway.py", line 1014, in send_command
    response = connection.send_command(command)
  File "D:\Program Files (x86)\Python27\lib\site-packages\py4j\clientserver.py", line 504, in send_command
    "Error while sending or receiving", e, proto.ERROR_ON_RECEIVE)
Py4JNetworkError: Error while sending or receiving
ERROR:py4j.clientserver:There was an exception while executing the Python Proxy on the Python Side.
Traceback (most recent call last):
  File "D:\Program Files (x86)\Python27\lib\site-packages\py4j\clientserver.py", line 581, in _call_proxy
    return_value = getattr(self.pool[obj_id], method)(*params)
  File "D:/fast-project/atsquant-project/sprint69-algopython/pingtest.py", line 16, in firstPong
    return player.secondPing(self)
  File "D:\Program Files (x86)\Python27\lib\site-packages\py4j\java_gateway.py", line 1286, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "D:\Program Files (x86)\Python27\lib\site-packages\py4j\protocol.py", line 336, in get_return_value
    format(target_id, ".", name))
Py4JError: An error occurred while calling o1.secondPing

py4f通过ClientServer使用单线程模式请求python服务

原文:https://www.cnblogs.com/xun-meng/p/12188440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!