问题描述
我正在构建一个 Flask api,它允许用户传递一个 xml 和一个转换,该转换返回使用 Saxon/C 的 python API (https://www.saxonica.com/saxon-c/doc/html/saxonc.html) 执行转换的 xml。
传入端点如下所示(删除了日志记录和无关信息):
@app.route("/v1/transform/",methods=["POST"])
def transform():
xml = request.data
transformation = request.args.get("transformation")
result = transform_xml(xml,transformation)
return result
变换函数如下所示:
def transform_xml(xml: bytes,transformation: str) -> str:
with saxonc.PySaxonProcessor(license=False) as proc:
base_dir = os.getcwd()
xslt_path = os.path.join(base_dir,"resources",transformation,"main.xslt")
xslt_proc = proc.new_xslt30_processor()
node = proc.parse_xml(xml_text=xml.decode("utf-8"))
result = xslt_proc.transform_to_string(stylesheet_file=xslt_path,xdm_node=node)
return result
xslt 在本地可用,用户应通过传递相应的 transformation
名称来选择可用的其中之一。
现在的问题是,这对第一个来电有效(快速),但第二个却崩溃了:
JNI_CreateJavaVM() Failed with result: -5
damN ! worker 1 (pid: 517095) died :( trying respawn ...
有效的是像这样改变 transform_xml 函数:
proc = saxonc.PySaxonProcessor(license=False)
xslt_path = self.__get_path_to_xslt(transformation)
xslt_proc = proc.new_xslt30_processor()
node = proc.parse_xml(xml_text=xml.decode("utf-8"))
result = xslt_proc.transform_to_string(stylesheet_file=xslt_path,xdm_node=node)
return result
但这会导致资源永远不会被释放,随着时间的推移(1k+ 请求),这会开始填满内存。
看起来 Saxon 正在尝试创建一个新的虚拟机,而旧的虚拟机正在宕机。
我在 2016 年发现了这个帖子:https://saxonica.plan.io/boards/4/topics/6399 但这并没有让我明白。我查看了 pysaxon repo 的 github,但我没有找到这个问题的答案。 还在撒克逊拍了一张票:https://saxonica.plan.io/issues/4942
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)