Why you might need to run Python scripts from Node.js
- Node js can't run heavy multitask as it works on a single thread basis, making it difficult for Node Js to execute CPU-bound tasks. Whenever any request is received, the system will complete the request once and only accept the other request. This process slows down the system and ends up in a massive delay.
- Python is much more suitable for that back-end applications, that use numerical computations, big data solutions, and machine learning.
- Python has one of the largest ecosystems out of any programming community and a lot of open-source libraries. For instance some of built-in AI libraries (Pylearn2, Tensorflow for neural networks, and Scikit-learn for data analysis).
Сall your Python script from Node.js using Child Process module
You will find several libraries that can help you with this issue. As for me, I prefer to use built in child_process. The node:child_process module provides the ability to spawn subprocesses. This capability is primarily provided by the child_process.spawn() function.
First, we need to create a server.js file as the server for our app. Where we suppose that x and y are large arrays.
Run Python script, that uses specific Python libraries
You will find several libraries that can help you with this issue. As for me, I prefer to use built in child_process. The node:child_process module provides the ability to spawn subprocesses. This capability is primarily provided by the child_process.spawn() function.
First, we need to create a server.js file as the server for our app. Where we suppose that x and y are large arrays.