How to call a Python function from Node.js

前端 未结 9 1638
借酒劲吻你
借酒劲吻你 2020-11-22 14:53

I have an Express Node.js application, but I also have a machine learning algorithm to use in Python. Is there a way I can call Python functions from my Node.js application

9条回答
  •  星月不相逢
    2020-11-22 15:11

    You can now use RPC libraries that support Python and Javascript such as zerorpc

    From their front page:

    Node.js Client

    var zerorpc = require("zerorpc");
    
    var client = new zerorpc.Client();
    client.connect("tcp://127.0.0.1:4242");
    
    client.invoke("hello", "RPC", function(error, res, more) {
        console.log(res);
    });
    

    Python Server

    import zerorpc
    
    class HelloRPC(object):
        def hello(self, name):
            return "Hello, %s" % name
    
    s = zerorpc.Server(HelloRPC())
    s.bind("tcp://0.0.0.0:4242")
    s.run()
    

提交回复
热议问题