发布于 2015-07-05 12:25:22 | 246 次阅读 | 评论: 0 | 来源: 网络整理

Node.js运行在一个单线程模式,但它使用一个事件驱动范例来处理并发。它还有助于创建子进程,以充分利用并行处理的多核CPU系统。

子进程总是有三个流child.stdin,child.stdout和child.stderr这可能与父进程stdio流共享。

Node提供child_process模块,该模块具有以下三个主要的方法来创建子进程。

  • exec - child_process.exec方法在shell/控制台运行一个命令并缓冲输出。

  • spawn - child_process.spawn启动一个新的过程,一个给定的指令

  • fork - child_process.fork方法是指定spawn()来创建子进程的一个特例。

exec() 方法

child_process.exec方法在shell运行一个命令并缓冲输出。它具有以下特征:


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

参数

下面是使用的参数的说明:

  • command 字符串是要运行的命令,用空格分隔的参数

  • options 对象可包括以下一个或多个选项:

    • cwd 子进程的字符串当前工作目录

    • env 对象环境的键值对

    • encoding 字符串(缺省:“UTF8”)

    • shell 字符串执行(默认命令:在UNIX上为 '/bin/sh',在Windows为cmd.exe“, 在shell应该明白/s /c 在Windows或 -c 在UNIX/;在Windows中,命令行解析应与cmd.exe兼容)

    • timeout 数字(默认: 0)

    • maxBuffer 数字(默认: 200*1024)

    • killSignal 字符串 (默认: 'SIGTERM')

    • uid 数字用于设置过程的用户的身份

    • gid 数字设置进程的组标识

  • callback 函数获取三个参数错误,输出和错误被称为与下面的输出,当进程终止。

在exec()方法返回一个缓冲带最大尺寸,等待结束该进程,并尝试一次返回所有缓存数据。

例子

让我们创建两个JS文件名分别为:support.js和master.js:

File: support.js


console.log("Child Process " + process.argv[2] + " executed." );

File: master.js


const fs = require('fs');
const child_process = require('child_process');

for(var i=0; i<3; i++) {
   var workerProcess = child_process.exec('node support.js '+i,
      function (error, stdout, stderr) {
         if (error) {
            console.log(error.stack);
            console.log('Error code: '+error.code);
            console.log('Signal received: '+error.signal);
         }
         console.log('stdout: ' + stdout);
         console.log('stderr: ' + stderr);
      });

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

现在运行master.js看到结果:


$ node master.js

验证输出。服务器已经启动


Child process exited with exit code 0
stdout: Child Process 1 executed.

stderr:
Child process exited with exit code 0
stdout: Child Process 0 executed.

stderr:
Child process exited with exit code 0
stdout: Child Process 2 executed.

spawn() 方法

child_process.spawn 方法启动使用给定命令一个新的进程。它具有以下特征:


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

参数

下面是使用的参数的说明:

  • command 字符串运行的命令

  • args 字符串参数数组列表

  • options 对象可包括以下一个或多个选项:

    • cwd 子进程的字符串为当前工作目录

    • env 对象环境的键值对

    • stdio 数组|子串的标准输入输出配置

    • customFds 数组,不推荐使用文件描述符的子数组,使用作为标准输入输出

    • detached 布尔将是一个子进程组

    • uid 数目设置进程的用户的身份。

    • gid 数目设置进程的组标识。

spawn()方法返回流(标准输出与标准错误),它当处理返回大量的数据被使用。spawn()开始接收到响应的进程开始执行。

例子

创建两个JS文件名分别为support.js和master.js:

File: support.js


console.log("Child Process " + process.argv[2] + " executed." );

File: master.js


const fs = require('fs');
const child_process = require('child_process');
 
for(var i=0; i<3; i++) {
   var workerProcess = child_process.spawn('node', ['support.js', i]);

   workerProcess.stdout.on('data', function (data) {
      console.log('stdout: ' + data);
   });

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

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

现在运行master.js看到的结果:


$ node master.js

验证输出。服务器已经启动


stdout: Child Process 0 executed.

child process exited with code 0
stdout: Child Process 1 executed.

stdout: Child Process 2 executed.

child process exited with code 0
child process exited with code 0

fork方法

child_process.fork方法是spawn()来创建节点的过程的一个特例。它具有以下签名


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

参数

下面是使用的参数的说明:

  • modulePath 字符串 - 该模块在运行子进程

  • args 字符串 - 参数数组列表

  • options 对象可包括以下一个或多个选项:

    • cwd 字符串 - 子进程的当前工作目录

    • env 对象环境的键值对

    • execPath 字符串 - 可执行用于创建子进程

    • execArgv 传递给可执行字符串参数数组列表(默认值:process.execArgv)

    • silent Boolean - 如果为true,标准输入,stdout和子标准错误将通过管道输送到父进程,否则会从父继承,看到“pipe”和“inherit”选项spawn()更多细节标准输入输出(默认为false)

    • uid 数字 - 设置进程的用户的身份。

    • gid 数字 - 设置进程的组标识。

fork 方法返回与对象内置除了具有在正常ChildProcess实例的所有方法的通信信道。

例子

创建两个JS文件名为support.js和master.js:

File: support.js


console.log("Child Process " + process.argv[2] + " executed." );

File: master.js


const fs = require('fs');
const child_process = require('child_process');
 
for(var i=0; i<3; i++) {
   var worker_process = child_process.fork("support.js", [i]);	

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

现在运行master.js看到的结果:


$ node master.js

验证输出。服务器已经启动


Child Process 0 executed.
Child Process 1 executed.
Child Process 2 executed.
child process exited with code 0
child process exited with code 0
child process exited with code 0
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务