Reputation: 565
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.
Now I need to find a way to communicate between Node.js and Python, and I already found two completely different approaches:
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?
Would somebody briefly explain what are the advantages and disadvantages with both approaches in this case ?
Upvotes: 4
Views: 1482
Reputation: 111434
There are pros and cons of both approaches.
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.
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.
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.
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.
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.
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.
Making a module can be as simple as putting the related code in a separate .js
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.
Upvotes: 2