Created
February 2, 2015 06:24
-
-
Save goodzsq/33f4e87d81a5de73c268 to your computer and use it in GitHub Desktop.
node.js koa框架的子进程调用
This file contains hidden or 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
| //父进程 | |
| 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