Skip to content

Instantly share code, notes, and snippets.

@matutter
Created June 13, 2014 03:06
Show Gist options
  • Save matutter/6e76c83cc2ac8f5819d8 to your computer and use it in GitHub Desktop.
Save matutter/6e76c83cc2ac8f5819d8 to your computer and use it in GitHub Desktop.
IPC in nodeJS
try {
var proc = cp.spawn('execs/a.out',['param1','param2'], null );
proc.stdout.setEncoding('utf-8');
proc.stdin.setEncoding('utf-8');
var writeBuffer = []
, artificial_latency = 50
, writeCRON = setInterval(function(){
if( /*proc.connected &&*/ writeBuffer.length ) {
proc.stdin.write( writeBuffer[0] + '\n')
console.log( 'buffer wrote ' + writeBuffer[0] )
writeBuffer.shift()
}
}, artificial_latency)
proc.stdout.on('data',function(stream){
console.log( proc.pid + ' >> ' + stream )
//writeBuffer.push( (Number(stream) + 100) )
})
proc.stderr.on('data', function(stream) {
console.log( stream )
})
proc.on('close', function(code) {
console.log('exit ' + code)
})
} catch(e) {
console.log(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment