将 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
已注册的 Listeners 的方法
实际上,当消息到达时,我收到此错误
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 实例,但是如何解决呢?我假设系统是多线程的,如Py4J 指南中所述。
可能是接口实现中的错误吗?谢谢你的帮助!