As I continue to log time working with node.js I start to get into things that are a little advanced such as clustering. When making a node.js project that will spawn additional instances of itself to help make some heavy lifting go faster, there is a need to know how many processors there are on the system that node is running. In this post I will be quickly writing about how to go abound finding that out very fast, and will be giving some quick examples on why this is helpful. This can be done with a method in the node.js os module know as os.cpus
This is a post on the os.cpus method in the os module, a helpful module that can be used to work with the underlaying operation system that node.js is running on top of. I will not be getting into the os module in detail in this post, but I will be coving some use case examples of the os.cpus method in this module. When I made these demos I was using node.js 8.x, which at the time of this writing is the latest LTS major release version of node.js.
Another helpful module in node.js is the cluster module, this helps setup clustering in a node.js project. That is running more than one instance of the same script. I wrote a post on cluster a while back if you would like to read more about clustering in node.js.
The os.cpus method works by just simply calling it with no arguments, and it then returns an array of objects, where each object contains some properties about each processor. So for a very basic example of os.cpus, I made a quick script that just logs out the model, and speed of each cpu object in the array of objects that is returned by is.cpus.
So the count of cpus on the system can be found by just getting the array element length of the array returned by os.cpus. In addition I can gain some additional information about each cpu, but it is not at all a replacement for some other kind of back end that would give more detailed information.
So for a more interesting example I made a quick example that involves the use of the cluster module. This module has many properties, and methods that help with forking a script. So it can be used to create more than one child process of the same script.
Of course I will not be getting into cluster in detail here, however I will be using os.cpus to launch a process for each processor that is found using os.cpus.
If I run this script in the command line The main process will fork two additional forks of this script. These two additional scripts will be worker scripts, rather than the master script that is called from the command line. In this example The workers just log a message to the console, and exit. However in a real example of some kind the worker scripts could do something that involves something that will take a lot of time, or processing power to complete. Each worker, and the main script are then free to do there own thing in parallel, and will not hold each other up.
So if I just want a count of how many processors there are to work with on a system the os.cpus methods works just fine, and with most of the scripts written with node.js there is not much need for detailed information about each processor. If you want more detailed information then you will want to use some other back end that can get that for you, and use it via the child-process module that can be used to work with such back ends from the command line withing a node.js script.