Skip to content

Instantly share code, notes, and snippets.

@julianduque julianduque/child.js
Created Aug 18, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.