我正在从Python调用Java jar文件。
def extract_words(file_path):
"""
Extract words and bounding boxes
Arguments:
file_path {[str]} -- [Input file path]
Returns:
[Document]
"""
extractor = PDFBoxExtractor(file_path=file_path,jar_path="external/pdfbox-app-2.0.15.jar",class_path="external")
document = extractor.run()
return document在某个地方:
pipe = subprocess.Popen(['java',
'-cp',
'.:%s:%s' %
(self._jar_path,
self._class_path) ,
'PrintTextLocations',
self._file_path],
stdout=subprocess.PIPE)
output = pipe.communicate()[0].decode()这工作得很好。但问题是jar很重,当我不得不在循环中多次调用它时,每次加载jar文件都需要3-4秒。如果我在一个循环中运行100次,它会增加300-400秒的时间。
有没有办法让java的类路径保持活动状态,而不是每次都加载jar文件?以时间最优化的方式做这件事的最好方法是什么?
发布于 2019-09-13 13:36:46
您可以将您的PDFBoxExtractor封装在一个类中,使其成为类的成员。在类的构造函数中初始化PDFBoxExtractor。如下所示:
class WordExtractor:
def __init__(self):
self.extractor = PDFBoxExtractor(file_path=file_path,jar_path="external/pdfbox-app-2.0.15.jar",class_path="external")
def extract_words(self,file_path):
"""
Extract words and bounding boxes
Arguments:
file_path {[str]} -- [Input file path]
Returns:
[Document]
"""
document = self.extractor.run()
return document下一步是在循环外部创建WordExtractor类的实例。
word_extractor = WordExtractor()
#your loop would go here
while True:
document = word_extractor.extract_words(file_path);这只是解释概念的示例代码。您可以根据您的要求以您想要的方式进行调整。
希望这能有所帮助!
https://stackoverflow.com/questions/57917676
复制相似问题