我需要知道如何从子进程到其父进程进行通信。我已经尝试过了:
在我的主应用中:
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 ready')永远不会被执行
发布于 2015-10-26 04:19:20
发送消息会触发"message“事件。所以你可以试试:
var cp = require('child_process');
var n = cp.fork('path/to/my/process', params);
n.on('message', function(msg) {
console.log('process is ready');
});请参阅https://nodejs.org/api/child_process.html#child_process_child_send_message_sendhandle_callback
发布于 2019-10-02 15:27:26
使用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.js
process.send('HELLO from child')
https://stackoverflow.com/questions/33334436
复制相似问题