1
第一代碼:如何產生獨立的孩子並重新建立node.js中的通信?
test.js:
var cluster = require("cluster");
if (cluster.isMaster) {
var app = cluster.fork();
app.on("message", function(code) {
console.log("Parent received: " + code);
app.send("this is from the test.js parent");
});
}
else {
process.send("");
process.on("message", function(code) {
console.log("Child received: " + code);
process.send("this is from the test.js child");
require("./testobj.js");
process.exit(0);
});
}
testobj.js:
process.send("this is from testobj.js");
process.on("message", function(code) {
console.log("testobj.js received: " + code);
});
process.send("this is the second message from testobj.js");
while (true) {}
當正在運行的節點test.js結果
Parent received:
Child received: this is from the test.js parent
Parent received: this is from the test.js child
Parent received: this is from testobj.js
Parent received: this is the second message from testobj.js
既然您看到我的工作到目前爲止,其目標是產生獨立於父代的子流程,但仍保留雙向通信。目前爲止的測試代碼保留了孩子與父母的溝通,但不是父母對孩子的溝通。 有沒有人對如何保留或重建父母與子女溝通有任何建議?