简体   繁体   English

父进程如何将socket / server对象发送到Node.js中的子进程?

[英]How can a parent process send socket/server object to child process in Node.js?

Consider: 考虑:

// Parent

var child = require('child_process').fork('child.js');

// Open up the server object and send the handle.
var server = require('net').createServer();
server.on('connection', function (socket) {
  socket.end('handled by parent');
});
server.listen(1337, function() {
  child.send('server', server);
});

//Child
process.on('message', function(m, server) {
  if (m === 'server') {
    server.on('connection', function (socket) {
      socket.end('handled by child');
    });
  }
});

As shown in above example, parents sends server object to child processes so that even a child had handle some of client connection requests. 如上例所示,父服务器将服务器对象发送到子进程,以便即使是子进程也处理了一些客户端连接请求。

How is it achieved in Node.js? 它是如何在Node.js中实现的?

Here is what node does when process.send is called with a handle argument . 以下是使用handle参数调用process.send时节点执行的操作

And after reading that, the handleConversion function is also interesting to read. 阅读完之后, handleConversion函数也很有趣。

I don't fully understand it yet, but I think essentially the state of the socket/server is serialized and passed between master/worker using IPC. 我还没有完全理解它,但我认为套接字/服务器的状态基本上是序列化的,并使用IPC在master / worker之间传递。 Maybe the state being passed around is enough for each process to bind itself to the socket? 也许传递的状态足以让每个进程将自己绑定到套接字? Or maybe the parent is keeping a registry of children who can handle any given socket, so once a new connection comes in for some socket, it is sent to a child that registered for it. 或者父母可能会保留一个可以处理任何给定套接字的子进程的注册表,所以一旦新连接进入某个套接字,它就会被发送给注册它的子进程。 That part I am less sure about. 那部分我不太确定。

This is now possible , but only if you're using the net module, which you are. 现在可以这样做了,但前提是你正在使用net模块。

child.send('socket', socket); Use the second parameter to send: 使用第二个参数发送:

And to receive from child process: 并从子进程接收:

process.on('message', function(m, socket) {
   if (m === 'socket') {
       console.log(socket); // Net socket object here
   }
});

I suggest you take a look at the Node.js cluster module : 我建议你看一下Node.js集群模块

When you call server.listen(...) in a worker, it serializes the arguments and passes the request to the master process. 在worker中调用server.listen(...)时,它会序列化参数并将请求传递给主进程。 If the master process already has a listening server matching the worker's requirements, then it passes the handle to the worker. 如果主进程已经具有与工作者要求匹配的侦听服务器,则它将句柄传递给工作者。 If it does not already have a listening server matching that requirement, then it will create one, and pass the handle to the child. 如果它还没有匹配该要求的侦听服务器,那么它将创建一个,并将句柄传递给子。

Complete example from the documentation : 文档中的完整示例:

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {
  // Workers can share any TCP connection
  // In this case it's an HTTP server
  http.createServer(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM