Node.js is single threaded and by default utilizes only one core of the CPU no matter how many cores the CPU has. Of course there are solutions of how to use all the cores but it's not part of this post. Just for the record, there are packages that does this job efficiently. Have a look at this link pm2.keymetrics.io/
Well, what am I trying to solve here? Imagine that you have 2 different endpoints on your application, and one of them needs a lot of processing time. What the problem would be?
Let's examine it;
The endpoints that we will use as examples are:
The above image depicts in nutshell, that the stack is busy and that /ping endpoint should wait until the heavy process is done.
Create a heavy processing controller
The code below creates a file with name "big.file" and adds 10.000.000 (or 1e7) times the text "blah blah". Once it is done we close the file and terminate the request
In Node.js we can use the child_process module which "provides the ability to spawn child processes in a manner that is similar, but not identical" (link). OK! Right! And how can we do this?
This module has quite a few methods but we will use only the fork method which spawn a new process and allow the parent to communicate with the child and exchange messages.
This means that the heavy task will be executed by another process and will send a message to parent process when it's done. Or whatever else we want to send
Below we fork the process by requesting the "compute.ts" file. (line 10)
The parent sends a message to the child (line 11)
The parent awaits for a message from the child to terminate the request (lines 12-14)
Below the process waits a "message" to be emitted (line 16) and executes the heavy task by invoking the constructor (line 17)
Once the heavy task is done, we send a message to the parent (line 18)
In this repo you can find the code that illustrates all the above
I am a senior software engineer who loves scuba diving and hiking. A dog type person who owns two cats