Java Python 集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1119696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Python Integration
提问by Jeff Storey
I have a Java app that needs to integrate with a 3rd party library. The library is written in Python, and I don't have any say over that. I'm trying to figure out the best way to integrate with it. I'm trying out JEPP (Java Embedded Python) - has anyone used that before? My other thought is to use JNI to communicate with the C bindings for Python.
我有一个需要与 3rd 方库集成的 Java 应用程序。该库是用 Python 编写的,我对此没有任何发言权。我试图找出与它集成的最佳方式。我正在尝试 JEPP(Java Embedded Python)——以前有人用过吗?我的另一个想法是使用 JNI 与 Python 的 C 绑定进行通信。
Any thoughts on the best way to do this would be appreciated. Thanks.
任何有关执行此操作的最佳方法的想法将不胜感激。谢谢。
采纳答案by Jon Skeet
Why not use Jython? The only downside I can immediately think of is if your library uses CPython native extensions.
为什么不使用Jython?我能立即想到的唯一缺点是您的库是否使用 CPython 本机扩展。
EDIT: If you can use Jython nowbut think you may have problems with a later version of the library, I suggest you try to isolate the library from your app (e.g. some sort of adapter interface). Go with the simplest thing that works for the moment, then consider JNI/CPython/etc if and when you ever need to. There's little to be gained by going the (painful) JNI route unless you really have to.
编辑:如果您现在可以使用 Jython,但认为您在使用更高版本的库时可能会遇到问题,我建议您尝试将库与您的应用程序隔离(例如某种适配器接口)。使用目前最简单的方法,然后在需要时考虑 JNI/CPython/etc。除非您真的必须这样做,否则走(痛苦的)JNI 路线几乎没有什么好处。
回答by ars
If you can get your Python code to work in Jython, then you should be able to use that to call it from Java:
如果您可以让您的 Python 代码在 Jython 中工作,那么您应该能够使用它从 Java 调用它:
回答by Ralphleon
I've investigated a similar setup with JNI. Maybe this will help if haven't seen it yet:
我已经使用 JNI 研究了类似的设置。如果还没有看到它,也许这会有所帮助:
http://wiki.cacr.caltech.edu/danse/index.php/Communication_between_Java_and_Python
http://wiki.cacr.caltech.edu/danse/index.php/Communication_between_Java_and_Python
回答by geowa4
You could use a messaging service like ActiveMQ. It has both Pythonand Java support. This way, you can leave the complicated JNI or C bindings as they are and deal solely with what I consider a simple interface. Moreover, when the library gets updated, you don't need to change much, if anything.
您可以使用像ActiveMQ这样的消息服务。它同时支持Python和 Java。这样,您可以保留复杂的 JNI 或 C 绑定,而只处理我认为的简单接口。此外,当库更新时,您不需要进行太多更改(如果有的话)。
回答by Marcin
Frankly most ways to somehow run Python directly from within JVM don't work. They are either not-quite-compatible (new release of your third party library can use python 2.6 features and will not work with Jython 2.5) or hacky (it will break with cryptic JVM stacktrace not really leading to solution).
坦率地说,以某种方式直接从 JVM 中运行 Python 的大多数方法都不起作用。它们要么不太兼容(您的第三方库的新版本可以使用 python 2.6 功能,并且不能与 Jython 2.5 一起使用)或 hacky(它会与神秘的 JVM 堆栈跟踪中断,并不会真正导致解决方案)。
My preferred way to integrate the two would use RPC. XML RPCis not a bad choice here, if you have moderate amounts of data. It is pretty well supported — Python has it in its standard library. Java libraries are also easy to find. Now depending on your setup either Java or Python part would be a server accepting connection from other language.
我更喜欢的集成两者的方法是使用 RPC。如果您有适量的数据,XML RPC在这里不是一个糟糕的选择。它得到了很好的支持——Python 在它的标准库中有它。Java 库也很容易找到。现在,根据您的设置,Java 或 Python 部分将成为接受来自其他语言的连接的服务器。
A less popular but worth considering alternative way to do RPCs is Google protobuffers, which have 2/3 of support for nice rpc. You just need to provide your transport layer. Not that much work and the convenience of writing is reasonable.
一种不太流行但值得考虑的替代方法来进行 RPC 是 Google protobuffers,它有 2/3 的支持nice rpc。您只需要提供您的传输层。没有那么多的工作和写作的方便是合理的。
Another option is to write a C wrapper around that pieces of Python functionality that you need to expose to Java and use it via JVM native plugins. You can ease the pain by going with SWIG SWIG.
另一种选择是围绕需要向 Java 公开并通过 JVM 本机插件使用它的 Python 功能部分编写 C 包装器。您可以通过使用 SWIG SWIG来缓解疼痛 。
Essentially in your case it works like that:
基本上在你的情况下它是这样工作的:
- Create a SWIG interface for all method calls from Java to C++.
- Create C/C++ code that will receive your calls and internally call python interpreter with right params.
- Convert response you get from python and send it via swig back to your Java code.
- 为从 Java 到 C++ 的所有方法调用创建一个 SWIG 接口。
- 创建 C/C++ 代码,该代码将接收您的调用并使用正确的参数在内部调用 python 解释器。
- 转换您从 python 获得的响应并通过 swig 将其发送回您的 Java 代码。
This solution is fairly complex, a bit of an overkill in most cases. Still it is worth doing if you (for some reason) cannot afford RPCs. RPC still would be my preferred choice, though.
这个解决方案相当复杂,在大多数情况下有点矫枉过正。如果您(出于某种原因)负担不起 RPC,仍然值得这样做。不过,RPC 仍然是我的首选。
回答by dfa
My other thought is to use JNI to communicate with the C bindings for Python.
我的另一个想法是使用 JNI 与 Python 的 C 绑定进行通信。
I like very much JNA:
我非常喜欢JNA:
JNA provides Java programs easy access to native shared libraries (DLLs on Windows) without writing anything but Java code—no JNI or native code is required. This functionality is comparable to Windows' Platform/Invoke and Python's ctypes. Access is dynamic at runtime without code generation.
JNA 为 Java 程序提供了对本地共享库(Windows 上的 DLL)的轻松访问,而无需编写除 Java 代码之外的任何内容——不需要 JNI 或本机代码。此功能可与 Windows 的 Platform/Invoke 和 Python 的 ctypes 相媲美。访问在运行时是动态的,无需生成代码。
My 0.02$ :)
我的 0.02 美元 :)
回答by bluenote10
Many years later, just to add an option which is more popular these days...
许多年后,只是为了添加一个现在更受欢迎的选项......
If you need CPython functionality, py4jis a good option. py4j has seen seen frequent updates in 201620172018 and has gained some popularity, because it is used e.g. by Apache Spark to achieve CPython interoperability.
如果您需要 CPython 功能,py4j是一个不错的选择。py4j 已经看到频繁更新2016年2017年2018 年并且已经获得了一些流行,因为它被 Apache Spark 用于实现CPython 互操作性。
回答by Pascal Fares
The best solutions, is to use Python programs throw REST API. You define your services and call them. You perhaps need to learn some new modules. But you will be more flexible for futures changes.
最好的解决方案,是使用 Python 程序抛出 REST API。您定义您的服务并调用它们。您可能需要学习一些新模块。但是你会更灵活地应对期货的变化。
Here a small list of use full modules for this purpose: Python modules
这里有一小部分用于此目的的完整模块:Python 模块
- Flask
- Flask-SQLAlchemy
- Flask-Restful
- SQlite3
- Jsonify
- 烧瓶
- Flask-SQLAlchemy
- Flask-Restful
- SQlite3
- jsonify
Java modules (for calling rest api) Jersey or Apache CXF
Java 模块(用于调用 rest api) Jersey 或 Apache CXF
You will need a small Learning curve, but later you will get more productivity and modularity and even elasticity...
您将需要一个小的学习曲线,但稍后您将获得更高的生产力和模块化,甚至弹性......
回答by vishwampandya
These are some of the tools which make it easier to bridge the gap between Python and Java:
以下是一些可以更轻松地弥合 Python 和 Java 之间差距的工具:
1.JythonPython implemented in Java
1. Java 实现的JythonPython
2.JPypeAllows Python to run java commands
2. JPype允许 Python 运行 java 命令
3.JeppJava embedded Python
3. JeppJava 嵌入式 Python
4.JCCa C++ code generator for calling Java from C++/Python
4. JCC一个用于从 C++/Python 调用 Java 的 C++ 代码生成器
5.Javabridgea package for running and interacting with the JVM from CPython
5. Javabridge一个用于从 CPython 运行并与 JVM 交互的包
6.py4jAllows Python to run java commands.
6. py4j允许 Python 运行 java 命令。
7.vocPart of BeeWare suite. Converts python code to Java bytecode.
7. vocBeeWare 套件的一部分。将 python 代码转换为 Java 字节码。
8.p2jConverts Python code to Java. No longer developed.
8. p2j将 Python 代码转换为 Java。不再开发。