Node.js to Python communication - server or child process?
<p>I am currently at a project that is mainly written in Node.js, which involves non-linear curve fitting. After trying to do this task with Node.js itself, I gave up on it, because it is simply impractical. So I was looking for a high level language for solving mathmatical problems like that one I am facing. I had to decide between MATLAB and Python, but since Python has really powerful methods by now and it's free, I decided to go with Python. </p>

<p>Now I need to find a way to communicate between Node.js and Python, and I already found two completely different approaches:</p>

<ol>
<li>Setting up a Python server, that solves the mathematical problem as described <a href="http://stackoverflow.com/questions/10775351/combining-node-js-and-python">here</a></li>
<li>Or spawning a child process from my node.js code as described <a href="http://www.sohamkamani.com/blog/2015/08/21/python-nodejs-comm/" rel="nofollow">here</a></li>
</ol>

<p>Now I would usually go with the client server approach because in my opinion it is cleaner, because it separates both languages. But since somebody is writing a blog post about the second approach, there must be some advantage with it, right? </p>

<p>Would somebody briefly explain what are the advantages and disadvantages with both approaches in this case ?</p>

<p>There are pros and cons of both approaches.</p>

<h2>Separate server</h2>

<p>Setting up a server is more time consuming, you need to make sure that the Python server is started before the Node app needs to talk with it, you have to restart it if it stops etc.</p>

<p>On the other hand you have a nicely separated service that can be used by other apps and can be easily moved to a separate box or a set of boxes if you need more performance.</p>

<h2>Spawning a process</h2>

<p>Spawning a process is much easier than running a separate server, you always know that it's running when you need it. You don't have to manage a separate server with start up scripts, respawning etc.</p>

<p>On the other hand you are restricted to run the Python program on the same machine as your Node program and if the performance is a problem, then you will have to make it a separate server to be able to run it on a different machine or a set of machines.</p>

<h2>Choice</h2>

<p>The choice is really a matter of your own expectations on the future usage and server load. Both approaches will work and both will have different strong and weak sides.</p>

<h2>Abstraction</h2>

<p>In any case, it may be useful to abstract away that choice in a form of a module, so that your main code wouldn't need to know which choice you made. It will mean that you can change your mind in the future.</p>

<p>Making a module can be as simple as putting the related code in a separate <code>.js</code> file and requiring that file from your main code. That module can export one or more functions that take a callback or return a promise and your main code doesn't need to know what happens under the hood as long as the callback gets called or the promise gets resolved with the expected data.</p>

python node.js client-server ipc