简体   繁体   English

Node.js使用生成两个子进程,然后两个子进程如何相互交换?

[英]Node.js use spawn two child processes, then how two child processes exchange with each other?

I have a ELF program named "A", "A" is a tcp server, and I have a ELF program named "B", "B" is a tcp client. 我有一个名为“ A”的ELF程序,“ A”是一个tcp服务器,我有一个名为“ B”的ELF程序,“ B”是一个tcp客户端。 When "B" begins to run, It will send msg to the server without judges if the server is ready. 当“ B”开始运行时,它将向服务器发送消息,而无需判断服务器是否已准备就绪。

Now I have to use Node.js to run "A" and "B" as two child process, how can I make the "A" must run early than "B"? 现在,我必须使用Node.js将“ A”和“ B”作为两个子进程运行,如何使“ A”必须比“ B”早运行?

I impl like this: 我暗示是这样的:

socket.on('xxx', function() {    
    var A = spawn("A", ...);     
    ...      
    var B = spawn("B", ...);     
    setTimeout(B.sendToA, 500);       
    ...   
});

Is there any good Ideas? 有什么好主意吗?

Thank you for your help! 谢谢您的帮助!

Since you mentioned that the server outputs some information on it's startup, you could listen for this and then start the client when it's up. 由于您提到服务器在启动时会输出一些信息,因此您可以侦听此信息,然后在启动时启动客户端。

Something like: 就像是:

socket.on('xxx', function() {    
    var A = spawn("A", ...);
    A.stdin.write("yyy");
    A.stdout.on('data', function(e) {
        if(e.toString() === "yyy") {
            var B = spawn("B", ...);
            B.sendToA(); //I'm guessing this is a pseudo-method?
        }
    });
});

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

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