2

我正在考虑将 node.js 嵌入到 python 中以将 node.js 功能添加到我现有的 python 代码中的选项。我知道它可以通过其他方式完成,如本文所述所述。但是,我希望尽可能多地保持现有 Python 项目的完整,这意味着允许 Python 管理执行。

PyV8 做了我想要的一切,除了提供一个类似 node.js 的环境,允许我在 PyV8 中使用 node.js 模块,所以它似乎是一个很好的起点。

node.js 是否提供类似于 V8 的外部 API,以便可以修改 PyV8 以包装 node.js?如果没有,有没有办法将 node.js 环境加载到 PyV8 中,以便我可以使用 node.js 模块?

谢谢!

4

2 回答 2

4

What you want to do is not supported. Unlike, say, the CPython interpreter, or even the V8 JavaScript interpreter, Node.js was not designed to be embedded, has no interface for doing so, and no serious plan to change that.

I can't find any official documentation on that, but there are numerous threads like this one that give the relevant information.

But that doesn't mean it's impossible. The top layer of Node isn't that complicated, and really you'd just need to patch out a few parts of it to do different things. And, in fact, people have tried to do exactly that, in projects like tacnode. I don't know if any of them are ready for prime time or not, but it may be worth looking at them, especially if you're willing and able to contribute if they're incomplete.

Of course that only gets you embedding in C or C++; you still need to embed in Python. But wrapping a C shared library so you can use it in Python (well, CPython and PyPy) is a long-solved problem; Python has had extension modules since almost the beginning, as well as ctypes and cffi if you don't want to write any C code. And there are third-party projects like Cython to let you write almost-Python code that directly calls your shared library as if it were native C, and compiles to a Python extension module.

So, it's almost certainly doable, but it's probably not going to be trivial, or packaged and ready to go out of the box.

于 2014-09-17T22:03:44.117 回答
1

不要嵌入。相反,让 python 和 Node 在两个不同的进程中并在它们之间进行通信。以 RabbitMQ 为例,它有 Node 和 Python 的客户端。

于 2014-09-18T06:26:02.103 回答