//parent.js
const cp = require('child_process');
const n = cp.fork(`${__dirname}/child.js`);
console.log('send from parent')
n.on('message', (m) => {
console.log('PARENT got message:', m);
});
n.send({ hello: 'world' });
//child.js
console.log('send from child')
process.on('message', (m) => {
console.log('CHILD got message:', m);
});
process.send({ foo: 'bar' });
现象:
如果注释调parent中的message上方的console语句,执行结果为:
send from child
CHILD got message: { hello: 'world' }
PARENT got message: { foo: 'bar' }
如果放开parent中的console,执行结果为:
send from parent
send from child
PARENT got message: { foo: 'bar' }
CHILD got message: { hello: 'world' }
也就是会影响父子进程中message事件的执行顺序,为什么会有这个问题,是ipc进程通信本身的问题还是node中事件循环的机制问题?先行谢过解答的大佬