一个被分叉的child_process是否可以作为父项,并且也可以分叉新进程?

在我的电子应用程序的用户界面(UI)中,有一个button,点击后使用child_process.fork()旋转另一个脚本。 这个新的孩子使用process.send()来传递状态消息来更新进度的UI。 这一切都很好

然而在孩子的过程中,有时候需要平行地做一些工作(Stage6)。 在Stage6中,我使用相同的child_process.fork()方法来启动8个工作。 debugging消息表明,这8名工人确实叉正好; 我在一个数组中保留对它们的引用,一切似乎都很好。

问题是,一旦这八名工人分手, 我再也没有听到他们的消息 。 他们使用process.send()发送的消息永远不会被Stage6接收。 就像他们不存在

现在,这里有趣的部分! 在Electron之外,我创build了一个testing脚本(使用CLI上的Node)以与Stage6完全相同的方式启动这8个工作人员,他们工作得很好,testing脚本听到他们的消息。 所以问题不在于工人,而在于如何推出。

在Electron中,自己分叉的一个进程(在我的例子中是一个用户界面)是否也是一个“父母”? 我们可以在叉子里面叉吗? 还是那太分散了?

是的,一个child_process也可以是一个“父”,并fork另一个child_process 。 OP的问题必须在别处。 考虑这三个文件:

1.js

const cp = require("child_process"); const path = require("path"); console.log(`[f1] 1 has started; now forking 2...`); let f2 = cp.fork(path.normalize(`${__dirname}\\2`)); f2.on("message", (message) => { console.log(`[f2] ${message}`); }); f2.on("exit", () => { console.log(`[f1] f2 just exited, so 1 is too.`); }); 

2.js

 const cp = require("child_process"); const path = require("path"); process.send(`This is 2 starting up; now forking 3`); let f3 = cp.fork(path.normalize(`${__dirname}\\3`)); f3.on("message", (message) => { console.log(`[f3] ${message}`); }); f3.on("exit", () => { console.log(`[f2] f3 just exited, so I am too.`); process.exit(); }); 

3.js

 process.send(`This is 3 starting up, and now shutting down`); process.exit(); 

运行“node 1.js”会产生如下的输出,certificate2.js既可以是1.js的subprocess,也可以是3.js的父进程

 [f1] 1 has started; now forking 2... [f2] This is 2 starting up; now forking 3 [f3] This is 3 starting up, and now shutting down [f2] f3 just exited, so I am too. [f1] f2 just exited, so 1 is too.