我正在使用Py4J在Python中使用Java回调函数。
Java openjdk 11.0.3
Python 3
Py4J 0.10.8.1
我过去已经成功使用Py4J在Python中使用Java回调函数。但是,我最近使用与以前相同的模式添加了一个新的回调类,但是现在我在新类中遇到以下错误。
AttributeError: 'JavaMember' object has no attribute '_get_object_id'
在试图找出问题的过程中,我打印了Java对象,
蟒蛇。旧类StatusCallback
显示的对象类型为TestsJNI$StatusCallback@548ebebc
,而新类显示的对象类型为<py4j.java_gateway.JavaMember object at 0x7f8dc69817f0>
。
这说明了我得到的AttributeError
。但是我不知道为什么我没有得到py4j.java_gateway.JavaMember
而不是TestsJNI$UploadCallback
之类的东西。
import py4j.GatewayServer;
public class TestsJNI
{
public static boolean functionCalled = false;
public static class UploadCallback
{
public static void callback()
{
functionCalled = true;
}
// I have also tried these functions
//public static void callback(int a)
//{
// functionCalled = true;
//}
//public static void callback(int a, boolean b)
//{
// functionCalled = true;
//}
}
public static UploadCallback uploadCallback = new UploadCallback();
public static class StatusCallback
{
public static void callback()
{
functionCalled = true;
}
}
public static StatusCallback statusCallback = new StatusCallback();
public static void main(String args[])
{
TestsJNI testApp = new TestsJNI();
// Py4J server
GatewayServer server = new GatewayServer(testApp);
server.turnLoggingOff();
server.start();
}
}
from py4j.java_gateway import JavaGateway, GatewayParameters, get_field
javaCmd = ("java -cp /mnt/c/Workspace/tests/java/:.:/home/fred/.local/share/py4j/py4j0.10.8.1.jar TestsJNI")
print(javaCmd)
self.jvmProcess = Popen(javaCmd, shell=True, preexec_fn=os.setsid)
time.sleep(1.0)
# Connect to JVM via Py4J gateway
logging.getLogger("py4j").setLevel(logging.ERROR)
self.gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_field=True))
entryPoint = self.gateway.entry_point
self.jni = entryPoint.autoguidanceJNI
self.jniObj = self.gateway.entry_point
testObj = self.jniObj.uploadCallback
print("upload testObj = " + str(testObj))
ret = False
testObj = self.jniObj.StatusCallback
print("status testObj = " + str(testObj));
upload testObj = <py4j.java_gateway.JavaMember object at 0x7f8dc69817f0>
status testObj = TestsJNI$StatusCallback@548ebebc
为什么Py4J给我一个py4j.java_gateway.JavaMember
类的UploadCallback
对象和一个TestsJNI$StatusCallback
类的StatusCallback
对象?