Search code examples
node.jslinuxprocesschild-process

Running multiple instances of the same script NodeJS - Child Processes


I have a script which queries an API, the input to the script is a unique user ID which will determine what endpoint is queried on the API.

I want to spawn multiple instances of this script for the given X number of users and wondering what is the best solution for this?

I have looked into NodeJS child processes, is this the main way of solving this problem within node, spawning multiple child processes from a main process? Are all these processes then running on one thread I assume as NodeJS is single threaded?

The script that I want to spawn multiple process's of will be running constantly once it is started, querying an API for data every second. I guess it will have to factor in how much compute power I have available but generally how scalable is the child processes way of doing things?

Also in the background would a NodeJS child process be doing the same thing as if I was to run a bash command index.js & and spawn different processes? (Aside from being able to control the child processes then from the main process in NodeJS)


Solution

  • What you require can be done using a process manager like pm2.

    You can read up more about pm2 here.

    To launch multiple instances as a cluster using pm2 with the following syntax.

    pm2 start server.js -i 4  
    

    The above command would launch 4 instances of server.js. Also note that you do a lot of configuration using a config file (read docs for it) and it even supports multi-threading.