如何创建多个节点。js命令行进程彼此对话?

时间:2022-01-15 01:00:30

I am messing around with https://github.com/nodejitsu/forever, and am wondering in general if and how you send messages from one command line process to another.

我一直在使用https://github.com/nodejitsu/forever,并且我想知道您是否以及如何将消息从一个命令行进程发送到另一个命令行进程。

On the web, you use HTTP to send requests back and forth. But I've never had to do that type of thing with the command line before. I know it probably has to do with ports and sockets, but not so sure where to start…

在web上,您使用HTTP来回发送请求。但是我以前从来不用命令行做这种事情。我知道这可能与端口和套接字有关,但不确定从哪里开始……

Here's the basic setup:

基本设置:

You have a mainProcess.js and childProcess.js, and they are invoked independently from the command line. Somehow, mainProcess.js must run a callback whenever childProcess.js sends it a message.

你有一个mainProcess。js和childProcess。它们是独立于命令行调用的。不知何故,mainProcess。当子进程时,js必须运行一个回调。js发送消息给它。

./parent.js:

var forever       = require("forever");
var mainProcess   = forever.start(["node", "mainProcess.js"], {max: 1, silent: true});
mainProcess.on("stdout", function(data) {
  console.log(data.toString().trim());
});
mainProcess.on("stderr", function(data) {
  console.log(data.toString().trim());
});
forever.startServer(mainProcess);

./child.js:

var forever       = require("forever");
var childProcess  = forever.start(["node", "childProcess.js"], {max: 1, silent: true});
childProcess.on("stdout", function(data) {
  // *send to parent process*
});
forever.startServer(childProcess);

./childProcess.js

console.log("A Message from a child process!")

One way to do it is to parse log files, but that seems like it'd get messy quick. It seems like there's some protocol out there that will allow me to send direct messages from one process to another the same way you would with HTTP to a url. How would you accomplish this?

一种方法是解析日志文件,但这似乎很快就会变得一团糟。似乎有一些协议可以让我从一个进程发送直接消息到另一个进程,就像你用HTTP发送到url的方式一样。你将如何做到这一点?

1 个解决方案

#1


3  

Performing IPC in node.js can be accomplished in a number of ways. Built in, there is the child_process.fork() API: http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork

在节点执行IPC。js可以通过多种方式实现。内置了child_process.fork() API: http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork

In forever, there is an option, .fork which will tell forever to use the child_process.fork() API instead of child_process.spawn():

在forever中,有一个选项.fork,它将告诉永远使用child_process.fork() API而不是child_process.spawn():

var child = forever.start('script-using-fork-api.js', {
  max: 1,
  fork: true,
  silent: true
});

If you're looking for IPC mechanisms which go over the network checkout:

如果你正在寻找IPC机制通过网络检查:

#1


3  

Performing IPC in node.js can be accomplished in a number of ways. Built in, there is the child_process.fork() API: http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork

在节点执行IPC。js可以通过多种方式实现。内置了child_process.fork() API: http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork

In forever, there is an option, .fork which will tell forever to use the child_process.fork() API instead of child_process.spawn():

在forever中,有一个选项.fork,它将告诉永远使用child_process.fork() API而不是child_process.spawn():

var child = forever.start('script-using-fork-api.js', {
  max: 1,
  fork: true,
  silent: true
});

If you're looking for IPC mechanisms which go over the network checkout:

如果你正在寻找IPC机制通过网络检查: