
正文
nodejs(二)child_process模块
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.child_process是Node.js的一个十分重要的模块,通过它可以实现创建多进程,以利用多核计算资源。
child_process
模块提供了四个创建子进程的函数,分别是
spawn
,
exec
,
execFile
和
fork
。其中
spawn
是最原始的创建子进程的函数,其他三个都是对
spawn
不同程度的封装。
spawn
只能运行指定的程序,参数需要在列表中给出,相当于
execvp
系统函数,而
exec
可以直接运行复杂的命令。
child_process.spawn(command, [args], [options])
child_process.exec(command, [options], callback)
例如要运行
ls -lh /usr
,使用
spawn
需要写成
spawn('ls', ['-lh', '/usr'])
,而
exec
只需
exec('ls -lh /usr')
。
exec
的实现原理是启动了一个系统shell来解析参数,因此可以是非常复杂的命令,包括管道和重定向。
此外,
exec
还可以直接接受一个回调函数作为参数,回调函数有三个参数,分别是
err
,
stdout
,
stderr
,非常方便直接使用,例如:
require('child_process').exec( 'ls -lh /usr' , function(err, stdout , stderr ) {
console.log( stdout );
});
如果使用
spawn
,则必须写成:
2.
fork
函数用于直接运行Node.js模块,例如
fork('./child.js')
,相当于
spawn('node', ['./child.js'])
。
与默认的
spawn
不同的是,
fork
会在父进程与子进程直接建立一个IPC管道,用于父子进程之间的通信。例如
var n = require('child_process').fork( './child.js');
n. on ( 'message', function(m) {
console. log ( 'PARENT got message:', m);
});
n.send({ hello: 'world' });
child.js的内容
process. on ( 'message', function(m) {
console. log ( 'CHILD got message:', m);
});
process.send({ foo: 'bar' });
结果:
PARENT got message: { foo: 'bar' }
CHILD got message: { hello: 'world' }
3.
fork
函数有一个问题,就是它只能运行JavaScript代码,如果你喜欢用CoffeeScript(或者其他任何编译到js的语言),是无法通过
fork
调用的。
一个简单的方法是把代码编译到JavaScript再运行,但是很不方便,有没有什么办法呢?答案是可以的,
child_process.spawn(command, [args], [options])
通过把
options
参数的
stdio
设为
['ipc']
,
即可在父子进程之间建立IPC管道。例如子进程使用CoffeeScript:
child_process = require ('child_process');
options ={stdio: ['ipc'] };
3 child = child_process.spawn('coffee', ['./child.coffee'], options);
其中只要把
spawn
的第一个参数设置为运行对应脚本的解释器,即可运行,例如使用Continuation.js,
只需
child = child_process.spawn('continuation', ['./child.coffee'], options)
。





