问题描述
在从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:
在我的应用程序中性能至关重要,因为我正在使用机器学习框架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大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!