Skip to content

Instantly share code, notes, and snippets.

@julianduque
Created August 18, 2017 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianduque/7ef21b96f7c751604a9dfec5620a2a83 to your computer and use it in GitHub Desktop.
Save julianduque/7ef21b96f7c751604a9dfec5620a2a83 to your computer and use it in GitHub Desktop.
fork example
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)
}
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