2018年的文章移至github上,点我去!2018年的文章移至github上,点我去!2018年的文章移至github上,点我去!

Fork me on GitHub

NodeJs之child_process

 

一.child_process


child_process是NodeJs的重要模块。帮助我们创建多进程任务,更好的利用了计算机的多核性能。

当然也支持线程间的通信。

 

二.child_process的几个API


异步:

child_process.exec(command[, options][, callback])

child_process.execFile(file[, args][, options][, callback])

child_process.fork(modulePath[, args][, options])

child_process.spawn(command[, args][, options])

同步:

child_process.execFileSync(file[, args][, options])

child_process.execSync(command[, options])

child_process.spawnSync(command[, args][, options])

事件:

Event: 'close'

Event: 'disconnect'

Event: 'error'

Event: 'exit'

Event: 'message'

 

三.child_process.spawn(command[, args][, options])


command:只执行的命令

args:参数列表

options:环境变量

先用一下:查询磁盘大小

var child_process = require('child_process');
var spawn = child_process.spawn;

var wmic = spawn('wmic', ['DiskDrive', 'get', 'Size', '/value']);

wmic.stdout.on('data', function(data) {
     console.log('使用spawn方法输出: ' + data);
 });

wmic.stderr.on('data', function(data) {
     console.log('stderr: ' + data);
});

wmic.on('close', function(code) {
     console.log('child process exited with code ' + code);
});

上面的命令在cmd中:wmic DiskDrive get Size /value

Node 通过 child_process 模块提供了类似 popen(3) 的处理三向数据流(stdin/stdout/stderr)的功能。

spawn()与exec(),execFile()的区别是:后两个创建时可以指定timeout属性设置超时时间,一旦创建的进程运行超过设定的时间将会被kill。

 

四.child_process.exec(command[, options][, callback])


exec添加了对shell命令的解析,可以执行复杂的命令。不需要像spawn一样分开写参数。并且有一个回调。

直接使用:wmic DiskDrive get Size /value

var child_process = require('child_process');
var exec = child_process.exec;

exec('wmic DiskDrive get Size /value', function (error, stdout, stderr) {
   if (error) {
     console.log(error.stack);
     console.log('Error code: '+error.code);
     return;
   }
   console.log('使用exec方法输出: '+stdout);
   console.log(`stderr: ${stderr}`);
});

如果没出错,error参数为null,退出码为0.只要不为0,就出错。

 

五.child_process.execFile(file[, args][, options][, callback])


不执行shell.

使用:

var execFile = require('child_process').execFile;
var child = execFile('node', ['--version'], (error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  console.log(stdout);
});

 

六.child_process.fork(modulePath[, args][, options])


不同于spawn,fork函数会在进程间建立通讯通道。

使用:父子进程的通信。这个经常使用!

parent.js

//主进程
var childProcess = require('child_process');
var child = childProcess.fork('./child.js');

//接受来自子进程的消息
n.on('message', function(msg) {
  console.log('来自子进程的消息: ', msg);
});

//发送消息给子(fork)进程
n.send({ hello: 'zqz' });

 

child.js

//子进程
//接受来自父进程的消息
process.on('message', function(msg) {
  console.log('收到父进程的消息:', msg);
});

//向父进程发送消息
process.send({ Hello: 'Mr.zhao' });

 

七.close 事件


“关闭”事件在会在所有stdio流子进程关闭时候触发。这是有别于“退出”的事件,因为多个进程可以共享相同的stdio流。

 

八.disconnect 事件


在子进程或父进程中使用使用.disconnect()方法后,这个事件会被触发,在断开之后,就不可能再相互发送信息了。

可以通过检查子进程的child.connected属性是否为true去检查是否可以发送信息。

 

九.error 事件


触发的条件:

1.进程不能被创建, 或者

2.进程不能被终止掉, 或者

3.由任何原因引起的数据发送到子进程失败.

 

十.exit 事件


这个事件是在子进程被结束的时候触发的. 假如进程被正常结束,‘code’就是退出进程的指令代码, 否则为'null'. 假如进程是由于接受到signal结束的, signal 就代表着信号的名称, 否则为null.

 

十一.message 事件


通过.send()发送的信息可以通过监听'message'事件获取到。

 

posted on 2016-11-19 14:43  qize  阅读(4830)  评论(1编辑  收藏  举报

导航