Combining node.js and Python Combining node.js and Python python python

Combining node.js and Python


This sounds like a scenario where zeroMQ would be a good fit. It's a messaging framework that's similar to using TCP or Unix sockets, but it's much more robust (http://zguide.zeromq.org/py:all)

There's a library that uses zeroMQ to provide a RPC framework that works pretty well. It's called zeroRPC (http://www.zerorpc.io/). Here's the hello world.

Python "Hello x" server:

import zerorpcclass HelloRPC(object):    '''pass the method a name, it replies "Hello name!"'''    def hello(self, name):        return "Hello, {0}!".format(name)def main():    s = zerorpc.Server(HelloRPC())    s.bind("tcp://*:4242")    s.run()if __name__ == "__main__" : main()

And the node.js client:

var zerorpc = require("zerorpc");var client = new zerorpc.Client();client.connect("tcp://127.0.0.1:4242");//calls the method on the python objectclient.invoke("hello", "World", function(error, reply, streaming) {    if(error){        console.log("ERROR: ", error);    }    console.log(reply);});

Or vice-versa, node.js server:

var zerorpc = require("zerorpc");var server = new zerorpc.Server({    hello: function(name, reply) {        reply(null, "Hello, " + name, false);    }});server.bind("tcp://0.0.0.0:4242");

And the python client

import zerorpc, sysc = zerorpc.Client()c.connect("tcp://127.0.0.1:4242")name = sys.argv[1] if len(sys.argv) > 1 else "dude"print c.hello(name)


For communication between node.js and Python server, I would use Unix sockets if both processes run on the same server and TCP/IP sockets otherwise. For marshaling protocol I would take JSON or protocol buffer. If threaded Python shows up to be a bottleneck, consider using Twisted Python, whichprovides the same event driven concurrency as do node.js.

If you feel adventurous, learn clojure (clojurescript, clojure-py) and you'll get the same language that runs and interoperates with existing code on Java, JavaScript (node.js included), CLR and Python. And you get superb marshalling protocol by simply using clojure data structures.


If you arrange to have your Python worker in a separate process (either long-running server-type process or a spawned child on demand), your communication with it will be asynchronous on the node.js side. UNIX/TCP sockets and stdin/out/err communication are inherently async in node.