如何保持Java虚拟机运行并发送请求

发布于 2025-01-21 05:29:50 字数 1621 浏览 1 评论 0原文

我是Java的初学者,我想将计划请求发送到 planner

问题在于,由于我每次都在启动jvm,执行时间很大!

您能告诉我如何保持JVM作为服务器运行并发送请求,而不是每次启动jvm以减少执行时间?提前致谢。

我正在使用python来调用jvm这样:

    def solver( 
        libraryPath='PDDL/pddl4j-devel/build/libs/pddl4j-3.8.3.jar',
        planner = 'fr.uga.pddl4j.planners.htn.stn.tfd.TFDPlanner',
        Memory = ['-Xms12288m' ,'-Xmx12288m'],
        domainPath  = 'new_domain.pddl',
        problemPath = 'problem_main.pddl',
        save=False
        ):
        """
        Function: solver, to run the solver and get a plan if there is one.
        ---
        Parameters:
        @param: planner, string, planner name.
        @param: libraryPath, string, the path to the java library(extension included)
        @param: Memory, list of two ['-Xms size m', -Xmx size m'].
        @param: domainPath, string, the path to the domain, (extension included).
        @param: problemPath, string, the path to the problem, (extension included).
        @param: save, boolean, to save the plan as log file.
        ---
        @return: None.
        """
        cmd = ['java', '-javaagent:'+libraryPath, '-server' , Memory[0] , Memory[1], planner, '-d', self.path+domainPath, '-p', self.path+problemPath]
        result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)

I'm a beginner in Java, and I want to send planning requests to a planner.

The problem is that the execution time is huge due to the fact that I'm launching the JVM at each time!

Can you please tell me how to keep the JVM running as a server and send requests instead of launching the JVM each time to reduce the execution time? thanks in advance.

I'm using python to call the JVM like this:

    def solver( 
        libraryPath='PDDL/pddl4j-devel/build/libs/pddl4j-3.8.3.jar',
        planner = 'fr.uga.pddl4j.planners.htn.stn.tfd.TFDPlanner',
        Memory = ['-Xms12288m' ,'-Xmx12288m'],
        domainPath  = 'new_domain.pddl',
        problemPath = 'problem_main.pddl',
        save=False
        ):
        """
        Function: solver, to run the solver and get a plan if there is one.
        ---
        Parameters:
        @param: planner, string, planner name.
        @param: libraryPath, string, the path to the java library(extension included)
        @param: Memory, list of two ['-Xms size m', -Xmx size m'].
        @param: domainPath, string, the path to the domain, (extension included).
        @param: problemPath, string, the path to the problem, (extension included).
        @param: save, boolean, to save the plan as log file.
        ---
        @return: None.
        """
        cmd = ['java', '-javaagent:'+libraryPath, '-server' , Memory[0] , Memory[1], planner, '-d', self.path+domainPath, '-p', self.path+problemPath]
        result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2025-01-28 05:29:50

开销的启动时间小于50%。我知道这不是理想的选择,但是除非您优化计划师,否则我不会担心。 ]

如前所述,构建长期运行的Java服务器应用程序是另一种选择。有许多框架,例如春季。

对于您来说,这可能是个过度杀伤力,因此您可以检查一些有关构建“简单http服务器”的教程,例如

tps:// docs .oracle.com/javase/tutorial/networking/sockets/index.html“ rel =“ nofollow noreferrer”> https://docs.oracle.com/javase.com/javase/javase/tutorial/networior/networlorial/sockets/sockets/index.htex.html

The startup time overhead is less than 50%. I understand that it's not ideal but unless you optimize your planner quite a bit I wouldn't be worried that much. ]

Building a long-running java server app is another option as already mentioned. There are numerous frameworks for that such as Spring.

For you, that's likely an overkill so you can check some tutorials about building "simple http server", e.g.

Or go even lower level with sockets: https://docs.oracle.com/javase/tutorial/networking/sockets/index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文