首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在不加载类路径的情况下从Python调用Java

在不加载类路径的情况下从Python调用Java
EN

Stack Overflow用户
提问于 2019-09-13 13:20:13
回答 1查看 45关注 0票数 2

我正在从Python调用Java jar文件。

代码语言:javascript
运行
复制
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

在某个地方:

代码语言:javascript
运行
复制
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文件?以时间最优化的方式做这件事的最好方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2019-09-13 13:36:46

您可以将您的PDFBoxExtractor封装在一个类中,使其成为类的成员。在类的构造函数中初始化PDFBoxExtractor。如下所示:

代码语言:javascript
运行
复制
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类的实例。

代码语言:javascript
运行
复制
word_extractor = WordExtractor()

#your loop would go here
while True:
    document = word_extractor.extract_words(file_path);

这只是解释概念的示例代码。您可以根据您的要求以您想要的方式进行调整。

希望这能有所帮助!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57917676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档