Вот пример из документа nodejs.
родитель. js
const cp = require('child_process');
const n = cp.fork(`${__dirname}/sub.js`);
n.on('message', (m) => {
console.log('PARENT got message:', m);
}); // Why use n the subprocess here????
// Causes the child to print: CHILD got message: { hello: 'world' }
n.send({ hello: 'world' });
ребенок. js
process.on('message', (m) => {
console.log('CHILD got message:', m);
});
// Causes the parent to print: PARENT got message: { foo: 'bar', baz: null }
process.send({ foo: 'bar', baz: NaN });
Я понимаю дочерний процесс использует process.on('message'...)
и process.send(..)
для получения и отправки сообщений родительскому процессу.
Но почему родительский процесс использует экземпляр подпроцесса для получения сообщений от дочернего процесса n.on('message'....)
. Должен ли он быть process.on('message'...)
как дочерний процесс?
Спасибо.