python - 运行代码两次时 JPype 将无法正确编译

标签 python anaconda jpype

我的代码:

jarLocation = "C:/Users/LahiruGunawardhana/Desktop/New folder/info/infodynamics.jar"
# Start the JVM (add the "-Xmx" option with say 1024M if you get crashes due to not enough memory space)

jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=" + jarLocation,"-Xmx256m")

teCalcClass = jpype.JPackage("infodynamics.measures.discrete").TransferEntropyCalculatorDiscrete
teCalc = teCalcClass(2,1)
teCalc.initialise()

noOfEdges=0
for i in range(0,N-1):
  for j in range(i+1,N):
   # print(i,"-->",j)
    if(nodes_connectivity(j,i) | nodes_connectivity(i,j)):

     sss = [int(i) for i in SsourceArray]
     ddd = [int(i) for i in DdestArray]
     teCalc.addObservations(sss, ddd)
     e=teCalc.computeAverageLocalOfObservations()

jpype.shutdownJVM()

对于此代码,我仅获取一次值。当我第二次运行此代码时,我收到一条错误消息:

_jpype.startup(jvm, tuple(args), True)

OSError: JVM is already started

不知何故,当我为每个步骤重新启动 python 控制台时,它都会给出值。

最佳答案

startJVM()之前检查isJVMStarted(),所以它会是这样的:

if jpype.isJVMStarted():
    return

jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=" + jarLocation,"-Xmx256m")

因此,如果您运行两次并且 JVM 正在运行此代码,将避免再次初始化它

关于python - 运行代码两次时 JPype 将无法正确编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52644215/

相关文章:

python - 属性错误: __enter__ when using numpy nditer inside the with statement

python - 将数据从 python 管道传输到外部命令

python - 在使用 conda 创建的不同环境中切换 jupyter 笔记本时遇到问题

java - python jpype 从java类的实例中获取值

python - 如何安装带有 .whl 文件的 Python 包?

java - 有人成功使用 JPype 加载 Netflix 光子类吗?

python - 类似于 cumsum 的一系列列之间的差异

Python-elasticsearch.exceptions.RequestError

python - Jupyter lab提示token;我怎么找到它?

python - 不永久保存环境