Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - child_process and cluster confusion

Take this brief example: I have a file called parent.js, with the following code:

var child_process = require('child_process')
var forker = child_process.fork(__dirname + '/child.js')

forker.on('message', function (msg) {
console.log('PARENT got message:', msg)
})

// sends a message to the forked process?
forker.send({msg: 'Parent message.'})

First question: am I getting this right? child_process.fork() returns the forker process, doesn't it? (like child_process.spawn()?)

Anyway, here's the code for child.js:

process.on('message', function (msg) {
console.log('CHILD got message:', msg)
})

// sends a message to the forker process? why?
process.send({msg: 'Message from the child.'})

Second question: what does process refer to inside the child process? To the current forked process I guess? If so, when I call process.send() I'm sending a message to the parent process right?

Third question: take this example (simplified version from Node: Up and Running):

var cluster = require('cluster')

if (cluster.isMaster) {
    // fork child...
    var worker = cluster.fork()
    worker.on('message', function (msg) {
        // do stuff
    })
} else if (cluster.isWorker) {
    process.send(aMessage)
}

What I find unclear is: worker is kind of the forker of the previous example? And process.send() inside the worker sends a message to the forker process?

like image 533
whatyouhide Avatar asked Aug 24 '13 10:08

whatyouhide


People also ask

What is the difference between cluster and Worker_threads packages in node js?

Clusters of Node. js processes can be used to run multiple instances of Node. js that can distribute workloads among their application threads. When process isolation is not needed, use the worker_threads module instead, which allows running multiple application threads within a single Node.

What is the difference between node js child process and clusters?

In a single thread, the individual instance of node. js runs specifically and to take advantage of various ecosystems, a cluster of node. js is launched, to distribute the load. With the help of a cluster module, child processes can be created very easily sharing the server ports.

Can you cluster multiple node processors?

A cluster module executes the same Node. js process multiple times. Therefore, the first thing you need to do is to identify what portion of the code is for the master process and what portion is for the workers.

What is cluster mode in Nodejs?

The cluster module enables creating child processes (workers) that run simultaneously while sharing the same server port. Every child process has its own event loop, memory, and V8 instance. The child processes use interprocess communication to communicate to the main parent Node. js process.


1 Answers

1) child_process.fork() returns the forked process in the same way as child_process.spawn() returns the newly spawned process. Indeed, fork() is just

[...] a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in.1

2) process in the child refers to the child process. Also,

In the child the process object will have a send() method, and process will emit objects each time it receives a message on its channel.2

So from within the child we can send messaged with 'send()' and we can receive them with .on('message'). Like in your snippet.

3) As stated in the documentation about the cluster module on Node.js:

The worker processes are spawned using the child_process.fork method, so that they can communicate with the parent via IPC and pass server handles back and forth. 3

So you're right. Node cluster wraps the functionality for process in a more usable way (please pay attention that at this point in time, the cluster module is marked as experimental. Api may change in the near future).

like image 137
danielepolencic Avatar answered Oct 13 '22 04:10

danielepolencic