我正在尝试使用Django为我的nlp项目构建API。我有JAR文件,并在Python中使用Jpype在JVM中运行它。它与第一个POST一起工作得很好,但是与第二个post方法一起崩溃,没有任何异常。
我认为是导致JVM的原因,并尝试了以下代码,但没有成功:
if not jp.isJVMStarted():
print('JVM starting...')
jp.startJVM(jp.getDefaultJVMPath(), "-Djava.class.path=%s" % JAR_PATH)
TurkishMorphology = jp.JClass('zemberek.morphology.TurkishMorphology')
Paths = jp.JClass('java.nio.file.Paths')
else:
print('JVM shutting down...')
jp.shutdownJVM()
print('JVM starting...')
jp.startJVM(jp.getDefaultJVMPath(), "-Djava.class.path=%s" % JAR_PATH)
我所有的代码:
def getMorfoloji(sentence):
JAR_PATH = r'bin\zemberek-full.jar'
if not jp.isJVMStarted():
print('JVM starting...')
jp.startJVM(jp.getDefaultJVMPath(), "-Djava.class.path=%s" % JAR_PATH)
TurkishMorphology = jp.JClass('zemberek.morphology.TurkishMorphology')
Paths = jp.JClass('java.nio.file.Paths')
else:
print('JVM shutting down...')
jp.shutdownJVM()
print('JVM starting...')
jp.startJVM(jp.getDefaultJVMPath(), "-Djava.class.path=%s" % JAR_PATH)
TurkishMorphology = jp.JClass('zemberek.morphology.TurkishMorphology')
Paths = jp.JClass('java.nio.file.Paths')
morphology = TurkishMorphology.createWithDefaults()
analysis = morphology.analyzeSentence(sentence)
results = morphology.disambiguate(sentence, analysis).bestAnalysis()
analysis_result = ''
for i in results:
analysis_result += str(i)
analysis_result += ' -- '
print(analysis_result)
jp.shutdownJVM()
return analysis_result
我如何继续运行JVM而不停止运行?