想问一个报错信息 看不太懂,是利用jpype调用java的包,但是第一次启动JVM虚拟机可以,第二次运行就报错
jvm already started
参考了这篇文章
是利用使用多进程,另起一个进程来加载jar包,在得到想要的结果后杀掉进程
但是现在报错
$ flask run
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
JVM has been shutdown
Traceback (most recent call last):
File "c:\programdata\anaconda3\Lib\multiprocessing\queues.py", line 241, in _feed
obj = _ForkingPickler.dumps(obj)
File "c:\programdata\anaconda3\Lib\multiprocessing\reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <class 'jpype._jclass.java.lang.Double'>: attribute lookup java.lang.Double on jpype._jclass failed
想请教下这个报错信息
这个博主的函数
# 加载jar包
def demo(q, data):
jarpath = os.path.join(os.getcwd(), "info\\ner\c111.jar")
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % jarpath)
Test = jpype.JClass('xxx.Ner')
res = Test.nerSentence(data)
q.put(res)
我照着修改自己的
def Feature(q, data_dict):
current_path = os.getcwd()
jvmPath = getDefaultJVMPath() # current_path + "\\jre1.8.0_201\\jre\\bin\\server\\jvm.dll" # jvm.dll地址
jarPath = os.path.join(os.path.abspath('.'), current_path + "\\jarPath")
dependency = os.path.join(os.path.abspath('.'), current_path + "\\dependency")
# 启动虚拟机
startJVM(jvmPath, "-Djava.class.path=%s" % jarPath, "-Djava.ext.dirs=%s" % dependency)
FinalSearchRanker = JClass("com.go2map.search.FinalSearchRanker")
ranker = FinalSearchRanker()
MLFeatureMap = JClass("com.go2map.mlp.data.MLFeatureMap")
features = MLFeatureMap()
data_dict = data_dict["Feature"]['vFeaturesMap']
for key, value in data_dict.items():
eval('features.set%s' % key)(value)
result = ranker.predict(features)
# 这里?
q.put(result)
# 关闭虚拟机
shutdownJVM()
这个博主的视图
@index_blu.route('/question')
def question():
"""在视图中使用多进程调用demo函数"""
data = request.args.get('val')
q = multiprocessing.Queue()
p = multiprocessing.Process(target=java_loop, args=[q, data])
p.daemon = True
p.start()
a = q.get()
p.terminate()
return a
我参照修改的视图
@app.route('/', methods=['GET', 'POST']) #程序实例
def index():
form = NameForm()
a=session.get('name')
if form.validate_on_submit():
old_name = session.get('name')
c=form.name.data
c = '{' + c + '}'
e = eval(c)
q = multiprocessing.Queue()
p = multiprocessing.Process(target=Feature, args=(q, e))
p.daemon = True
p.start()
d = q.get()
p.terminate()
session['name'] = d
return redirect(url_for('index'))
return render_template('index.html', form=form, name=a)
想请教下问题出在哪里 应该如何用这个multiprocessing
找到一篇帖子 也看不太懂 请问是这里边的原因么