Created
January 24, 2012 03:21
-
-
Save cowboy/1667586 to your computer and use it in GitHub Desktop.
If a forked node process exits, how can the parent know?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| console.log('child spawned'); | |
| process.on('exit', function() { | |
| // This never executes. | |
| process.send('exited from within child'); | |
| }); | |
| function tellParentImDone() { | |
| process.send('hey parent, all done'); | |
| } | |
| // WHOOPS BROKEN 3RD PARTY CODE FORGOT TO DO THIS | |
| // tellParentImDone(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Run with: node parent.js | |
| var fork = require('child_process').fork; | |
| var child = fork('child.js'); | |
| child.on('exit', allDone.bind(null, 'exit')); | |
| child.on('message', allDone.bind(null, 'message')); | |
| function allDone(mode, arg) { | |
| console.log(mode + ':', arg); | |
| child.kill(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment