0

Suppose I have nodeJS cluster (master and workers). Number of workers is numCPU value. How can I send to each worker current data? For example I have array[1, 4].

  1. First worker must get array[1, 2]
  2. Second - array[2, 3]
  3. Third -array[3, 4]

and so on. Then I want to use each part in function that every worker do.

0

1 Answer 1

2

You can send messages to your children using child.send() and listen to messages from children using child.on('message', () => {...}). Vice-versa, you can send message from child to its parent process using process.send() and listen to messages from your parent using process.on('message', () => {}).

Here is the link to the full documentation.

The same goes for cluster module: https://nodejs.org/api/cluster.html#cluster_worker_send_message_sendhandle_callback

if (cluster.isMaster) {
  const worker = cluster.fork();
  worker.send({ some: 'data', arr: [1, 4] });
} else {
  process.on('message', data => {
    //do something with data
  }); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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