可用于各种 Python 的 Python 扩展(jython / IronPython / 等)

发布于 2024-08-18 22:40:05 字数 368 浏览 3 评论 0原文

在只有 cpython 的“过去”,大多数扩展都是用 c 编写的(尽可能独立于平台)并编译到 pyd 中(例如,想想 PyCrypto)。现在有 Jython、IronPython 和 PyPy,而 pyd 不能与它们中的任何一个一起工作(除了 Ironclad)。似乎它们都支持 ctypes,最好的方法可能是创建一个独立于平台的 dll 或共享库,然后使用 ctypes 与其连接。

但我认为这种方法会比老式的 pyd 方法慢一点。您还可以为 cpython 编写一个 pyd,为 IronPython 编写一个类似的 c# dll,为 Jython 编写一个 java 类或 jar(我不确定 PyPy。但是,虽然这种方法会吸引平台纯​​粹主义者,但它是非常劳动密集型的。所以什么是今天的最佳路线是?

In the 'old days' when there was just cpython, most extensions were written in c (as platform independent as possible) and compiled into pyd's (think PyCrypto for example). Now there is Jython, IronPython and PyPy and the pyd’s do not work with any of them (Ironclad aside). It seems they all support ctypes and that the best approach MIGHT be to create a platform independent dll or shared library and then use ctypes to interface to it.

But I think this approach will be a bit slower than the old fashion pyd approach. You could also program a pyd for cpython, a similar c# dll for IronPython and a java class or jar for Jython (I'm not sure about PyPy. But while this approach will appeal to platform purists it is very labor intensive. So what is the best route to take today?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-08-25 22:40:05

目前看来,ctypes 确实是最好的方法。今天它已经发挥作用,而且非常方便,它将征服(大部分)世界。

对于性能关键的API(例如numpy),ctypes确实是有问题的。最干净的方法可能是移植 Cython 以生成本机 IronPython / Jython / PyPy 扩展。

我记得 PyPy 计划将 ctypes 代码编译为高效的包装器,但据我谷歌搜索,还没有类似的东西......

Currently, it seems the ctypes is indeed the best approach. It works today, and it's so convenient that it's gonna conquer (most of) the world.

For performance-critical APIs (such as numpy), ctypes is indeed problematic. The cleanest approach would probably be to port Cython to produce native IronPython / Jython / PyPy extensions.

I recall that PyPy had plans to compile ctypes code to efficient wrappers, but as far as I google, there is nothing like that yet...

人间不值得 2024-08-25 22:40:05

如果您要包装现有的本机库,那么 ctypes 绝对是最佳选择。

如果您想加快 Python 扩展中的热点速度,那么为每个解释器(以及纯 Python 后备)创建自定义扩展是很容易处理的,因为大部分代码是纯 Python,可以共享,但不受欢迎正如你所说,而且是劳动密集型的。在这种情况下您也可以使用 ctypes。

If you're wrapping an existing native library, the ctypes is absolutely the way to go.

If you're trying to speed up the hot spots in a Python extension, then making a custom extension for each interpreter (and a pure-Python fallback) is tractable because the bulk of the code is pure Python that can be shared, but undesirable and labour-intensive, as you said. You could use ctypes in this case as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文