您的位置:首页 > Web前端 > Node.js

Node.js学习笔记(6、child_process模块)

2014-02-07 10:10 459 查看
child_process模块用于新建子进程。子进程的运行结果储存在系统缓存之中(最大200KB),等到子进程运行结束以后,主进程再用回调函数读取子进程的运行结果。

var childProcess = require('child_process');

var ls = childProcess.exec('ls -l', function (error, stdout, stderr) {
if (error) {
console.log(error.stack);
console.log('Error code: '+error.code);
}
console.log('Child Process STDOUT: '+stdout);
});

ls.on('exit', function (code) {
console.log('Child process exited with exit code '+code);
});


上面代码的exec方法会新建一个子进程,然后缓存它的运行结果,运行结束后调用回调函数。由于上面运行的是ls命令,它会自然结束,所以不会触发exit事件,因此上面代码最后监听exit事件的部分,其实是多余的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: