1

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)

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi sorry for the late, reply going to have a look into this now thanks for the suggestion! Still open to other solutions too if anyone has any.
Yep I have marked as answer this is the way to go, thanks for help!
You may be able to help with this one stackoverflow.com/questions/64910872/…
0

You can try this cli tool to achive the thing you need. The library will ask for a script to spawn and will cycle it's execution with multiple child process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.