py4f通过ClientServer使用单线程模式请求python服务
在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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?