我需要知道如何从子进程与其父进程进行通信。 我已经尝试过:
在我的主应用程序中:
var spawn = require('child_process').spawn
var cp = spawn('path/to/my/process', params)
cp.on('ready', function(){
console.log('process is ready')
})
在我的子进程应用程序中:
process.emit('ready')
但 console.log('process is read')
从未执行
最佳答案
使用process.send()方法从子级向父级发送消息。
// Parent process
const childProcess = require('child_process');
var process = childProcess.fork('child.js');
process.on('message', function (message) {
console.log('Message from Child process : ' + message);
});
在 child 身上
// child.js
process.send('HELLO from child')
关于node.js - 子进程向父进程发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33334436/