I am looking at the option of embedding node.js into python to add node.js functionality to my existing python code. I know that it can be done the other way around, as described in this post. However, I want to keep the as much of the existing Python project intact as possible, which means allowing Python to manage execution.
PyV8 does just about everything I want except provide a node.js-like environment that would allow me to use node.js modules in PyV8, so it seems like a good starting point.
Does node.js provide an external API similar to that of V8 such that one could modify PyV8 to wrap node.js? If not, is there a way to load the node.js environment into PyV8 so I can use node.js modules?
Thanks!
I am looking at the option of embedding node.js into python to add node.js functionality to my existing python code. I know that it can be done the other way around, as described in this post. However, I want to keep the as much of the existing Python project intact as possible, which means allowing Python to manage execution.
PyV8 does just about everything I want except provide a node.js-like environment that would allow me to use node.js modules in PyV8, so it seems like a good starting point.
Does node.js provide an external API similar to that of V8 such that one could modify PyV8 to wrap node.js? If not, is there a way to load the node.js environment into PyV8 so I can use node.js modules?
Thanks!
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Sep 17, 2014 at 21:28 ToasterToaster 1,9812 gold badges25 silver badges43 bronze badges 9- 4 what do you want to do that you can't just have a node service running side by side with your python script or just node by itself? – bitoiu Commented Sep 17, 2014 at 21:30
-
1
Instead of asking specifically about Python, you should first ask whether Node can be embedded into a C (or C++) application. If that's not possible, embedding it in Python (at least CPython, which is what you probably want) will not be possible; if it is, embedding it in Python will at worst be a matter of wrapping the C embedding in a Python C extension module or calling it via
ctypes
orcffi
– abarnert Commented Sep 17, 2014 at 21:56 -
1
Meanwhile, last I checked (which was a while ago…), Node (unlike CPython… or, for that matter, V8) had no official embedding support, and no plans to add any, but there were various third-party wrappers, like
tacnode
, that you can research. – abarnert Commented Sep 17, 2014 at 21:57 - @bitoiu Good question. You may be right, a service could be the solution. However, given the choice I would prefer a tight integration with node.js for manageability and speed. wrt Speed, Node.js will almost certainly be processor bound and spinning up a private instance will help a bit with that. wrt Manageability, I would rather not add an additional interface to the system. – Toaster Commented Sep 17, 2014 at 21:57
- 1 it's an overhead but on the system design, contrary to the overhead you might be creating on maintaining a feature (embedding) which no one else will understand and that could give you further problems in the future. – bitoiu Commented Sep 17, 2014 at 22:02
2 Answers
Reset to default 4What 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 plicated, 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 inplete.
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 piles 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.
Don't embed. Instead, have python and Node in two different processes and municate between them. RabbitMQ as an example has clients for both Node and Python.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745219666a4617186.html
评论列表(0条)