NodeJs之child_process

 

一.child_processnode


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

固然也支持线程间的通讯。异步

 

二.child_process的几个API函数


异步:性能

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

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

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

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

同步:blog

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'事件获取到。

相关文章
相关标签/搜索