Node.js中子进程之间的通信

时间:2017-08-10 23:03:13

标签: node.js parallel-processing fork communication child-process

我试图在node.js中创建一个使用fork() childproccess方法创建两个进程的程序。流程如下:

  • Father.js
  • Son1.js
  • Son2.js

我想直接在两个子进程之间传输数据,而不是在父进程和子进程之间传输数据。我向您展示了我想要做的事情。

communication between child proccess

我尝试使用以下代码,但它对我不起作用。

father.js 代码中,我按如下方式创建子进程:

const cp = require("child_process");
var son1 = cp.fork("${__dirname}/son1.js");
var son2 = cp.fork("${__dirname}/son2.js");

console.log("father sending message to son1..");
son1.send({msg:'Hi son1',br:son2});

console.log("father sending message to son2..");
son2.send({msg:'Hi son1',br:son1});

Son1.js 的代码:

var brother=null;
process.on('message', function(json)
{
  console.log('message father in son1.js;', json.msg);
  brother=json.br;
  brother.send("hello I'm son1.js"); 
});

Son2.js 的代码:

var brother=null;     
process.on('message', function(json)
{     
  console.log('message father in son2.js;', json.msg);       
  brother=json.br;
  brother.send("hello I'm son2.js");         
});

如何在不向son1.js发送消息的情况下,从son2.jsfather.js发送和接收消息,反之亦然?

4 个答案:

答案 0 :(得分:1)

您必须打开另一个通信渠道,例如本地套接字(tcp,udp或unix)或第三方服务(例如redis)。

答案 1 :(得分:1)

对于将数据发送到子进程,您可以使用此

const fork = require('child_process').fork('./son1.js')
fork.send('message')

son1.js
process.on('message', message => {
  console.log(message)
  process.exit()
})

答案 2 :(得分:0)

以下是您可以做的事情:

  1. 在链接到套接字文件的父级上创建IPC服务器。
  2. 打开与服务器的连接(仍然在父服务器内),创建套接字对。
  3. 将服务器的套接字发送给一个孩子,将客户端的套接字发送给另一个孩子。
  4. parent.js:

    const net = require('net');
    const cp = require('child_process');
    
    let u_proc_1 = cp.fork(__dirname+'/child.js', ['#1']);
    let u_proc_2 = cp.fork(__dirname+'/child.js', ['#2']);
    
    // create IPC server on parent
    let d_server = net.createServer((d_socket_2) => {
        // send server socket to #2
        u_proc_2.send('socket', d_socket_2);
    });
    
    // create socket file
    let p_socket = __dirname+'/sibling.sock';
    
    // bind server to socket file
    d_server.listen(p_socket);
    
    // create client socket; this also triggers creation of a server socket
    let d_socket_1 = net.connect(p_socket, () => {
        // have #1 send to #2
        u_proc_1.send('hey!');
    
        // have #2 send to #1
        u_proc_2.send('hello');
    });
    
    // send client socket to #1
    u_proc_1.send('socket', d_socket_1);
    

    child.js:

    const name = process.argv[2];
    
    let d_socket_sibling;
    
    process.on('message', (s_action, d_socket_msg) => {
        // parent is sending a socket
        if('socket' === s_action) {
            console.log(name+' now has a socket');
    
            // save socket to variable for later use
            d_socket_sibling = d_socket_msg;
    
            // receive data from sibling
            d_socket_sibling.on('data', (s_data) => {
                console.log(name+' received: '+s_data);
            });
        }
        // otherwise, parent wants me to send message to sibling
        else {
            console.log(name+' is sending: '+s_action);
    
            // send data to sibling
            d_socket_sibling.write(s_action);
        }
    });
    

    输出:

    #2 now has a socket
    #1 now has a socket
    #2 is sending: hello
    #1 is sending: hey!
    #1 received: hello
    #2 received: hey!
    

    结果是两个子进程之间的直接双向通信通道,由父进程存在。请记住,您必须以某种方式清理套接字文件(例如,在启动时或关闭父级之前删除它)

答案 3 :(得分:-1)

enter image description here

你可以查看这篇文章 See this link