Java 什么是 Jython,它到底有没有用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1859865/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:22:02  来源:igfitidea点击:

What is Jython and is it useful at all?

javapythonjython

提问by TIMEX

I know Python, but what is Jython?

我知道 Python,但什么是 Jython?

  • When will I need Jython?
  • What are the drawbacks?
  • I assume it is slow?
  • 我什么时候需要 Jython?
  • 有什么缺点?
  • 我认为它很慢?

Please detail it out! thanks.

请详细说明!谢谢。

采纳答案by Joey

Quoting Wikipedia:

引用维基百科

Jython, successor of JPython, is an implementation of the Python programming language written in Java.

Jython 是 JPython 的继承者,是用 Java 编写的 Python 编程语言的实现。

As for what uses it may have:

至于它可能有什么用途:

Jython programs can seamlessly import and use any Java class. Except for some standard modules, Jython programs use Java classes instead of Python modules.

Jython 程序可以无缝导入和使用任何 Java 类。除了一些标准模块,Jython 程序使用 Java 类而不是 Python 模块。

Performance measurements of Jython, IronPython and CPython show that both Jython and IronPython outperform CPython for some cases with large datasets.

Jython、IronPython 和 CPython 的性能测量表明,在某些具有大型数据集的情况下,Jython 和 IronPython 的性能都优于 CPython。

回答by Vitaliy Liptchinsky

Jython is a Python implementation which runs on top of JVM. Theoretically, Jython is a bit slower, then CPython.

Jython 是一个运行在 JVM 之上的 Python 实现。理论上,Jython 慢一点,然后是 CPython。

You can find some benchmarks here: http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5/

您可以在此处找到一些基准:http: //www.smallshire.org.uk/suffcientlysmall/2009/05/22/ironpython-2-0-and-jython-2-5-performance-compared-to-python-2 -5/

回答by wisty

The JVM does some nice dynamic optimization, but it's probably nothing to get excited about. JRuby sometimes runs faster on the JVM (http://www.engineyard.com/blog/2009/j-is-for-jvm-why-the-j-in-jruby/) by optimizing the code paths that run a lot, but that's Ruby, not Python.

JVM 做了一些不错的动态优化,但可能没什么好激动的。JRuby 有时在 JVM 上运行得更快(http://www.engineyard.com/blog/2009/j-is-for-jvm-why-the-j-in-jruby/)通过优化运行很多的代码路径,但那是 Ruby,而不是 Python。

Java libraries are the main reason. Lots of companies have big globs of Java code that can be scripted with Python. There's also a few open source frameworks (Struts and Eclipse spring to mind) which you can script with Jython.

Java 库是主要原因。许多公司都有大量可以用 Python 编写的 Java 代码。还有一些开源框架(Struts 和 Eclipse),您可以使用 Jython 编写脚本。

回答by Michael Borgwardt

When will I need Jython?

我什么时候需要 Jython?

When you want to program in Python but need (or want) to have the result run on a Java virtual machine, or use existing Java components.

当您想用 Python 编程但需要(或想要)在 Java 虚拟机上运行结果,或使用现有的 Java 组件时。

What are the drawbacks.

有什么缺点。

Jython may not be 100% compatible with Python, though any incompatibility would be considered a bug. If you later want/need to run on CPython, any code that uses Java components will have to be rewritten.

Jython 可能不会 100% 与 Python 兼容,但任何不兼容都将被视为错误。如果您以后想要/需要在 CPython 上运行,则必须重写任何使用 Java 组件的代码。

I assume it is slow?

我认为它很慢?

That depends, as always, on your specific usecase. It may actually be faster than CPython in some cases; and of course it depends on the specific JVM you run under - these get better all the time.

一如既往,这取决于您的特定用例。在某些情况下,它实际上可能比 CPython 更快;当然,这取决于您在特定的 JVM 下运行——这些一直在变得更好。

回答by Aaron Digulla

When will I need Jython?

我什么时候需要 Jython?

For example to add a nice scripting language to your code.

例如,为您的代码添加一种不错的脚本语言。

What are the drawbacks?

有什么缺点?

The main drawback is that Jython lags behind the official CPython distribution. Currently, you can get a version of Jython that is compatible with Python 2.5.2 while CPython is at 3.1.

主要缺点是 Jython 落后于官方 CPython 发行版。目前,您可以获得与 Python 2.5.2 兼容的 Jython 版本,而 CPython 是 3.1。

Also some esoteric modules aren't supported. Usually, you won't notice and/or be able to easily find a Java replacement.

也不支持一些深奥的模块。通常,您不会注意到和/或能够轻松找到 Java 替代品。

I assume it is slow?

我认为它很慢?

Compared to what? Usually, it's either fast enough or, when it isn't, you can replace a few lines of Python with about 1'000 lines of much faster Java.

与什么相比?通常,它要么足够快,要么当它不够快时,您可以用大约 1,000 行速度更快的 Java 替换几行 Python。

回答by Richie

Two other reasons:

另外两个原因:

  • Embedding scripting into large Java application.
  • Use Java threads to write multi-threaded programs in Jython.
  • 将脚本嵌入到大型 Java 应用程序中。
  • 使用 Java 线程在 Jython 中编写多线程程序。

回答by Priyabrata Hota

When will I need Jython?

我什么时候需要 Jython?

You can write administrative scripts for Websphere application server using Jython. The scripts are used to automate common administrative tasks and configure security in Websphere application server.

您可以使用 Jython 为 Websphere 应用程序服务器编写管理脚本。这些脚本用于自动执行常见的管理任务并在 Websphere 应用程序服务器中配置安全性。

WebSphere utilizes the JMX API to create MBeans that are exposed as an API of internal WebSphere objects. As the Jython scripts are based on Java and they run on the JVM they can access those objects and can be utilized to configure WebSphere.

WebSphere 利用 JMX API 创建作为内部 WebSphere 对象的 API 公开的 MBean。由于 Jython 脚本基于 Java 并且它们在 JVM 上运行,因此它们可以访问这些对象并可用于配置 WebSphere。

Besides it is fun to write code in Jython and you learn a new language.

此外,在 Jython 中编写代码并学习一门新语言很有趣。

Here is the details

这是详细信息

回答by Micha? Niklas

When will I need Jython?

我什么时候需要 Jython?

I need Jython to test JDBC drivers. Some of apps I use work use ODBC, some use JDBC. Using Jython I can test both type of drivers from one Python source (to test ODBC I use JDBC-ODBC bridge).

我需要 Jython 来测试 JDBC 驱动程序。我使用的一些应用程序使用 ODBC,一些使用 JDBC。使用 Jython,我可以从一个 Python 源测试两种类型的驱动程序(为了测试 ODBC,我使用 JDBC-ODBC 桥接器)。

回答by Ron

If you know Python and has bought into the "pythonic" way of doing things, then Jython allows you to bring that philosophy to the JVM stack. If you do this, it is much more than just adding scripting capability.

如果您了解 Python 并且已经接受了“pythonic”的做事方式,那么 Jython 允许您将这种理念引入 JVM 堆栈。如果您这样做,它不仅仅是添加脚本功能。

In our latest projects, all the custom and business logic is built in Jython, at the same time we can still leverage some of the great tried and tested Java libraries like Solr, Jasperreports, Quartz, Jetty, Velocity to name a few.

在我们最新的项目中,所有自定义和业务逻辑都是在 Jython 中构建的,同时我们仍然可以利用一些经过验证的优秀 Java 库,例如 Solr、Jasperreports、Quartz、Jetty、Velocity 等等。

It does get compiled to bytecode, however, an extra layer is being added, but is no different to using an ORM instead of straight JDBC for example.

它确实被编译为字节码,但是,添加了一个额外的层,但与使用 ORM 而不是直接的 JDBC 没有什么不同,例如。

What you gain in productivity far out weighs the minuscule lost in performance.

您在生产力方面获得的收益远远超过了性能方面的微小损失。

On the server side, Jython is rarely the bottleneck. For mini desktop apps, there may be issues, but very much dependent on what you are trying to do.

在服务器端,Jython 很少成为瓶颈。对于迷你桌面应用程序,可能存在问题,但这在很大程度上取决于您尝试做什么。

The latest JDK, together with containers like Jetty or Tomcat are very mature and stable, adding Python on top, in many cases, gives the best of both worlds.

最新的 JDK 连同 Jetty 或 Tomcat 等容器都非常成熟和稳定,在许多情况下,在顶部添加 Python 可以提供两全其美的效果。

回答by Lucky Pratama

To achieve a good speed performance or implementing a real multithreading program, calling python script directly from java (native) is the best way. Just prepare your python script then let java do the rest for concurrent invocation into your python script.

为了获得良好的速度性能或实现真正的多线程程序,直接从java(本机)调用python脚本是最好的方式。只需准备您的 python 脚本,然后让 java 完成其余的并发调用到您的 python 脚本中。