Wednesday, 19 April 2017

Why does my forked child process exit immediately after I fork it?

I'm just trying to fork a simple child process and have the IPC channel stay open but it keeps exiting immediately for some reason.

In parent.js:

var child = require('child_process').fork('./child.js');
child.on('hi', function() {
    console.log("Hi");
});

child.on('exit', function() {
    console.log("Exited");
});

child.send('hello');

In child.js:

process.on('hello', function() {
    process.send('hi');
});

I get "Exited" printed to the console immediately, and never get a 'Hi'. Then if I continue to try to send to the child process I get a channel closed error.

Something I am doing wrong?



via Joey

No comments:

Post a Comment