Skip to content

Instantly share code, notes, and snippets.

@goodzsq
Created February 2, 2015 06:24
Show Gist options
  • Select an option

  • Save goodzsq/33f4e87d81a5de73c268 to your computer and use it in GitHub Desktop.

Select an option

Save goodzsq/33f4e87d81a5de73c268 to your computer and use it in GitHub Desktop.
node.js koa框架的子进程调用
//父进程
var cp = require('child_process');
var forkChildProcess = function(sendData){
return function(cb){
var child = cp.fork('./server/childProcess.js');
child.on('message', function(msg){
cb(null, msg);
});
child.send(sendData);
}
}
//web接口
var handler = function*(){
var result = yield forkChildProcess({hello:'world'});
this.body = JSON.stringify(result);
}
//子进程
process.on('message', function(sendData) {
//处理发送过来的数据sendData
process.send({'echo': sendData});
//结束子进程
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment