Created
August 18, 2017 21:07
-
-
Save julianduque/7ef21b96f7c751604a9dfec5620a2a83 to your computer and use it in GitHub Desktop.
fork example
This file contains 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
const minimist = require('minimist') | |
const args = minimist(process.argv) | |
switch (args.name) { | |
case 'slave': | |
console.log('Doing things in the slave') | |
startSlave() | |
break | |
default: | |
console.log('Doing thins in the other thing') | |
startThing() | |
} | |
function startSlave () { | |
setInterval(function () { | |
process.send({ value: Math.random() }) | |
}, 1000) | |
} | |
function startThing () { | |
setInterval(function () { | |
process.send({ value: Math.random() }) | |
}, 1000) | |
} |
This file contains 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
const { fork } = require('child_process') | |
const workers = [ 'slave', 'thing', 'thing' ] | |
workers.forEach(function (w) { | |
ps = fork('./child.js', ['--name', w]) | |
ps.on('message', function (msg) { | |
console.log(`Received from ${w} - ${msg.value}`) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment