将py4J与多线程Java应用程序一起使用时遇到问题。
我已经用Python实现了一个Java接口
class PythonListener(object):
def __init__(self):
pass
def onNewMessage(self, message):
print(message)
class Java:
implements = ["com.myMessaging.IListener"]
我从Python启动JVM:
java_gateway = JavaGateway.launch_gateway(
die_on_exit = True)
java_import(java_gateway.jvm, 'com.myMessaging.*')
// Start the multithread messaging server
myMessaging.Server.start()
// Register the listener
myMessaging.Server.registerListener(PythonListener())
myMessaging
是一个Java多线程应用程序。当消息到达时,某个线程将接收它并将调用已注册监听器的onNewMessage
方法
事实上,当消息到达时,我收到此错误
py4j.Py4JException: Error while obtaining a new communication channel
at py4j.CallbackClient.getConnectionLock(CallbackClient.java:257)
at py4j.CallbackClient.sendCommand(CallbackClient.java:377)
at py4j.CallbackClient.sendCommand(CallbackClient.java:356)
at py4j.reflection.PythonProxyHandler.invoke(PythonProxyHandler.java:106)
at com.sun.proxy.$Proxy79.onNewMessage(Unknown Source)
at com.myMessaging.Server.notifyMessage(Server.java:235)
...Some stuff...
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:212)
at py4j.CallbackConnection.start(CallbackConnection.java:226)
at py4j.CallbackClient.getConnection(CallbackClient.java:238)
at py4j.CallbackClient.getConnectionLock(CallbackClient.java:250)
... 32 more
我认为这是因为有多个Java线程试图在同一个端口上连接到同一个Python实例,但是如何修复它?我假设系统是多线程的,如here in the Py4J guide所述。
这可能是接口实现中的错误吗? 谢谢你的帮助!
答案 0 :(得分:1)
使用launch_gateway时,不会启动负责执行Python回调的回调服务器。你必须明确地启动它:
java_gateway = JavaGateway.launch_gateway(die_on_exit = True)
java_gateway.start_callback_server()
# Your code here
请注意,这将使用Python回调的默认端口,但您正在使用短暂的端口进行Java调用。如果您想为Java和Python使用临时端口the solution presented in the advanced topics section of Py4J is a bit more convoluted。