Home  >  Article  >  Web Front-end  >  How to create a child process using node.js (detailed tutorial)

How to create a child process using node.js (detailed tutorial)

亚连
亚连Original
2018-06-21 17:33:512719browse

When I read the chapter on multi-process before, I found that there are a lot of things about it. I also mentioned it when I was writing the Process module. I woke up from my lunch break this afternoon and calmed down to read it carefully and found that it is not too difficult to understand. So the following article mainly introduces you to the relevant information on how to create a child process using node.js. Friends in need can refer to it.

Preface

Node itself is a single process and uses driver mode to handle concurrency. In order to solve the waste of resources of a single process on a multi-core CPU, Node provides the cluster and child_process modules to create multiple child processes.

Node.js is single-threaded, which is a waste for machines that are now generally multi-processor. How can it be used? So the child_process module appeared. The child_process module can spawn, fork, and perform work on other processes.

The child_process module provides a new class ChildProcess, which can be used as a representation of accessing the child process from the parent process. The Process module is also a ChildProcess object. When you access process from the parent module, it is the parent ChildProcess object. When you access Process from the child process, it is the ChildProcess object

Understanding an object is nothing more than events, methods, and properties. The same goes for ChildProcess.

Each child process always has three stream objects: child.stdin, child.stdout, child.stderr. They may share the stdio stream of the parent process.

Here we first introduce how to operate the child process using the three methods of exec, spawn, and fork in the child_process module.

Create the node-childProcess file and create the node-childPro.js file in it.

One line of code is as follows:

console.log("进程 " + process.argv[2] + " 执行。" );
//换成下面的查看process.argv
//console.log("进程 " + process.argv + " 执行。" );

exec() method

Create a new node- in the node-childProcess file childPro-exec.js file, the code is as follows:

const fs = require('fs');
const child_process = require('child_process');
for (var i = 0; i < 3; i++) {
 //这里有空格请注意。分别代表node路径 node-childPro.js路径 i第几个进程。 node-childPro.js中的process.argv可以获取这些信息值
 var childProcess = child_process.exec(&#39;node node-childPro.js &#39;+i,
 // 回调函数 子进程的输出以回调函数参数的形式返回
 function (error, stdout, stderr) {
  if (error) {
  console.log(error.stack);
  console.log(&#39;Error code: &#39; + error.code);
  console.log(&#39;Signal received: &#39; + error.signal);
  }
  console.log(&#39;stdout: &#39; + stdout);
  console.log(&#39;stderr: &#39; + stderr);
 });
 childProcess.on(&#39;exit&#39;, function (code) {
 console.log(&#39;子进程已退出,退出码 &#39; + code);
 });
}

The terminal execution code result is as follows:

G:\node\node-childProcess> node node-childPro-exec.js
子进程已退出,退出码 0
stdout: 进程 0 执行。
stderr:
子进程已退出,退出码 0
stdout: 进程 1 执行。
stderr:
子进程已退出,退出码 0
stdout: 进程 2 执行。
stderr:

spawn() method

Create a new node-childPro-spawn.js in the node-childProcess file, with the code as follows:

const fs = require(&#39;fs&#39;);
const child_process = require(&#39;child_process&#39;);
 for(var i=0; i<3; i++) {
 var childProcess = child_process.spawn(&#39;node&#39;, [&#39;node-childPro-spawn.js&#39;, i]);  
 childProcess.stdout.on(&#39;data&#39;, function (data) {
 console.log(&#39;stdout: &#39; + data);
 });
childProcess.stderr.on(&#39;data&#39;, function (data) {
 console.log(&#39;stderr: &#39; + data);
 });
 childProcess.on(&#39;close&#39;, function (code) {
 console.log(&#39;子进程已退出,退出码 &#39;+code);
 });
}

The terminal execution code results are as follows:

G:\node\node-childProcess> node node-childPro-spawn.js
stdout: 进程 0 执行。
子进程已退出,退出码 0
stdout: 进程 1 执行。
stdout: 进程 2 执行。
子进程已退出,退出码 0
子进程已退出,退出码 0

fork( )Method

Create a new node-childPro-fork.js in the node-childProcess file, with the code as follows:

const fs = require(&#39;fs&#39;);
const child_process = require(&#39;child_process&#39;);
 for(var i=0; i<3; i++) {
 var childProcess = child_process.fork("node-childPro.js", [i]); 
 childProcess.on(&#39;close&#39;, function (code) {
 console.log(&#39;子进程已退出,退出码 &#39; + code);
 });
}

The terminal execution code results are as follows:

G:\node\node-childProcess> node node-childPro-fork.js
进程 0 执行。
进程 1 执行。
子进程已退出,退出码 0
进程 2 执行。
子进程已退出,退出码 0
子进程已退出,退出码 0

About exec, spawn, fork

## 1. The exec function is a friendly encapsulation of spawn. It adds Shell command parsing and can directly embed complex Command

2. The exec function caches the output of the sub-process and returns the output of the sub-process in the form of callback function parameters

3. After the child thread starts executing, spawn begins to continuously Data is returned from the child process to the main process (application scenario such as "system monitoring")

4.spawn does not support the callback function. It sends data to the main process through a stream, thereby realizing multi-process Data exchange between

5. fork() is a special case of spawn(), used to derive the Node process. In addition to all the methods of a normal ChildProcess instance, the returned object also has built-in communication channels.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to implement component communication in vue-cli

Use angular to write the Message component

How to bind Json data source using EasyUI

The above is the detailed content of How to create a child process using node.js (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn