节点。js - child_process和集群混乱

时间:2022-08-02 00:06:19

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

举个简单的例子:我有一个名为parent的文件。js,代码如下:

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()?)

第一个问题:我做得对吗?fork()返回forker进程,不是吗?(如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?

第二个问题:进程在子进程中指什么?到现在的分叉过程?如果是,当我调用process.send()时,我向父进程发送消息,对吗?

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

第三个问题:以这个例子为例(简化版本来自Node: Up和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?

我发现不清楚的是:worker是前一个例子的分支?在worker中发送一个消息给forker进程?

1 个解决方案

#1


10  

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

1) child_process.fork()以与child_process.spawn()相同的方式返回分叉的进程。事实上,叉()是公正的

[...] 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

[…)生成节点进程的派生()函数的一个特殊情况。除了在普通的子进程实例中拥有所有方法之外,返回的对象还有一个通信通道build - In .1

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

2)子进程是指子进程。同时,

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

在子对象中,process对象将有一个send()方法,并且进程每次在其通道上接收消息时都将发出对象

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

因此,我们可以从子节点内部发送带有“send()”的消息,并通过.on(“message”)接收它们。就像在你的代码片段。

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

3)如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

工作进程是使用child_process生成的。fork方法,以便它们可以通过IPC与父进程通信,并来回传递服务器句柄。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).

所以你是对的。节点集群以一种更有用的方式包装进程的功能(请注意,此时集群模块被标记为experimental)。Api在不久的将来可能会改变)。

#1


10  

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

1) child_process.fork()以与child_process.spawn()相同的方式返回分叉的进程。事实上,叉()是公正的

[...] 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

[…)生成节点进程的派生()函数的一个特殊情况。除了在普通的子进程实例中拥有所有方法之外,返回的对象还有一个通信通道build - In .1

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

2)子进程是指子进程。同时,

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

在子对象中,process对象将有一个send()方法,并且进程每次在其通道上接收消息时都将发出对象

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

因此,我们可以从子节点内部发送带有“send()”的消息,并通过.on(“message”)接收它们。就像在你的代码片段。

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

3)如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

工作进程是使用child_process生成的。fork方法,以便它们可以通过IPC与父进程通信,并来回传递服务器句柄。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).

所以你是对的。节点集群以一种更有用的方式包装进程的功能(请注意,此时集群模块被标记为experimental)。Api在不久的将来可能会改变)。