Skip to main content
QUICK REVIEW

[论文解读] JyNI - Using native CPython-Extensions in Jython

Stefan Richthofer|arXiv (Cornell University)|Apr 25, 2014
Computational Physics and Python Applications被引用 5
一句话总结

JyNI 通过实现一个将 Python C-API 桥接到 Jython 的 JVM 运行时的兼容层,使 Jython 能够原生加载并执行 CPython 扩展(如 NumPy 和 SciPy),而无需重新编译或分叉,从而在不修改现有代码的前提下,实现了对 CPython 扩展的无缝集成,使科学计算工作负载能够在 Java 平台上运行。

ABSTRACT

Jython is a Java based Python implementation and the most seamless way to integrate Python and Java. However, it does not support native extensions written for CPython like NumPy or SciPy. Since most scientific Python code fundamentally depends on exactly such native extensions directly or indirectly, it usually cannot be run with Jython. JyNI (Jython Native Interface) aims to close this gap. It is a layer that enables Jython users to load native CPython extensions and access them from Jython the same way as they would do in CPython. In order to leverage the JyNI functionality, you just have to put it on the Java classpath when Jython is launched. It neither requires you to recompile the extension code, nor to build a customized Jython fork. That means, it is binary compatible with existing extension builds. At the time of writing, JyNI does not fully implement the Python C-API and it is only capable of loading simple examples that only involve most basic built-in types. The concept is rather complete though and our goal is to provide the C-API needed to load NumPy as soon as possible. After that we will focus on SciPy and others. We expect that our work will also enable Java developers to use CPython extensions like NumPy in their Java code.

研究动机与目标

  • 使基于 Java 的 Python 实现 Jython 能够原生加载并执行 CPython 扩展(如 NumPy 和 SciPy)。
  • 克服由于 C-API 不兼容导致 Jython 无法加载二进制 CPython 扩展的限制。
  • 提供与二进制兼容的解决方案,可直接使用现有的 CPython 扩展构建,无需代码更改或自定义 Jython 分支。
  • 通过支持核心科学计算库的访问,为工业级 Java 环境中运行科学 Python 工作负载奠定基础。
  • 实现对关键扩展(从 NumPy 及其依赖项开始)的 Python C-API 的完整兼容。

提出的方法

  • 实现一个兼容层(JyNI),将 CPython C-API 调用映射到 Jython 的基于 JVM 的运行时中的等效操作。
  • 利用 Jython 的钩子机制,在运行时将 JyNI 注入类路径,实现扩展加载的透明化。
  • 在 Java 中镜像 CPython 对象和桩对象,通过 JyGCHead 对象保持引用连接,以支持 Java 垃圾回收器对原生对象的管理。
  • 利用 Java 的引用机制(如带 ReferenceQueue 的 PhantomReference)来管理原生对象的清理,避免依赖 finalizers。
  • 通过类路径或系统库路径支持原生库的动态加载,实现对 libJyNI.so 和 libJyNI-loader.so 的自动发现。
  • 实现选择性 C-API 支持,从基本类型和关键模块(如 datetime 和 ctypes)开始,以支持 NumPy 的依赖链。

实验结果

研究问题

  • RQ1是否可以构建一个兼容层,使 Jython 在不修改扩展或 Jython 本身的情况下,加载并执行 CPython 二进制扩展?
  • RQ2如何在 JVM 环境中模拟 Python C-API,以支持像 NumPy 这类复杂的科学计算库?
  • RQ3哪些机制可以确保在混合 Java/Python 运行时中,镜像的 CPython 对象的内存管理与垃圾回收正确执行?
  • RQ4如何检测并清理涉及 Jython 和原生 CPython 对象的引用循环,以防止内存泄漏?
  • RQ5现有 CPython 扩展二进制文件在 Jython 中可复用的程度如何,且仅需极少修改?

主要发现

  • JyNI 成功使 Jython 能够加载并执行原生 CPython 的 datetime 模块,表现为正确的 "Fast implementation of the datetime type." 文档字符串和正确的对象行为。
  • 该解决方案与现有的 CPython 扩展构建完全二进制兼容,无需重新编译 Jython 或扩展,也无需分叉。
  • JyNI 支持通过标准类路径注入加载扩展,仅需在类路径中添加 JyNI.jar 和原生库,无需修改启动命令。
  • 该架构使用 JyGCHead 对象和基于 PhantomReference 的清理机制,将原生对象生命周期管理与 Java 垃圾回收器无缝集成。
  • JyNI 当前仅支持 Linux,但设计支持在核心兼容性实现后,未来扩展至跨平台。
  • 该方法使 Java 开发者能够通过 Jython 直接在 Java 应用中使用 CPython 扩展(如 NumPy)。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。