Py4J没有运行状况检查,ping或版本命令,所以测试Py4J是否正在监听的唯一三个选项是:
尝试连接到套接字.此方法并不完美,因为另一个服务器程序可能正在侦听默认端口.
尝试热切地加载JavaGateway(将在0.8版本中提供.现在只能在github上的master分支中使用):
from py4j.java_gateway import JavaGateway, Py4JNetworkError
try:
java_gateway = JavaGateway(eager_load=True)
print("JVM accepting connection")
except Py4JNetworkError:
print("No JVM listening")
except Exception:
print("Another type of problem... maybe with the JVM")
Run Code Online (Sandbox Code Playgroud)在Py4J 0.7中,尝试调用基本的System方法:
from py4j.java_gateway import JavaGateway, Py4JNetworkError
java_gateway = JavaGateway()
try:
java_gateway.jvm.System.getProperty("java.runtime.name")
print("JVM accepting connection")
except Py4JNetworkError:
print("No JVM listening")
except Exception:
print("Another type of problem... maybe with the JVM")
Run Code Online (Sandbox Code Playgroud)欢迎功能请求!