Py4J的开销比Jython和JPype大 [英] Py4J has bigger overhead than Jython and JPype

查看:221
本文介绍了Py4J的开销比Jython和JPype大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从Django应用程序(python)中搜索运行Java代码的选项之后,我发现Py4J对我而言是最佳选择.我尝试了Jython,JPype和Python子进程,它们每个都有一定的局限性:

After searching for an option to run Java code from Django application(python), I found out that Py4J is the best option for me. I tried Jython, JPype and Python subprocess and each of them have certain limitations:

  • Jython.我的应用程序在python中运行.
  • JPype越野车很容易.再次启动失败后,您可以启动JVM.
  • Python子进程.由于常规的控制台调用,无法在Python和Java之间传递Java对象.

在Py4J网站上写道:

On Py4J web site is written:

就性能而言,由于Py4J依赖于套接字,因此其开销要比之前的两个解决方案(Jython和JPype)都要大,但是如果性能对您的应用程序至关重要,则从Python程序访问Java对象可能不是最佳选择.想法.

In terms of performance, Py4J has a bigger overhead than both of the previous solutions (Jython and JPype) because it relies on sockets, but if performance is critical to your application, accessing Java objects from Python programs might not be the best idea.

在我的应用程序中性能至关重要,因为我正在使用机器学习框架Mahout.我的问题是:由于Py4J网关服务器,Mahout的运行速度也会变慢吗?否则,这意味着从Python函数调用Java方法的速度会变慢(在后者的情况下,Mahout的性能不会成为问题,我可以使用Py4J).

In my application performance is critical, because I'm working with Machine learning framework Mahout. My question is: Will Mahout also run slower because of Py4J gateway server or this overhead just mean that invoking Java methods from Python functions is slower (in latter case performance of Mahout will not be a problem and I can use Py4J).

推荐答案

JPype问题,其中提到的与JVM脱离新线程的@HIP_HOP可以通过以下技巧解决(将其添加到新的Java对象的第一次调用之前)没有JVM的线程):

JPype issue that @HIP_HOP mentioned with JVM getting detached from new threads can be overcome with the following hack (add it before the first call to Java objects in the new thread which does not have JVM yet):

# ensure that current thread is attached to JVM
# (essential to prevent JVM / entire container crashes 
# due to "JPJavaEnv::FindClass" errors)
if not jpype.isThreadAttachedToJVM():
    jpype.attachThreadToJVM()

这篇关于Py4J的开销比Jython和JPype大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆