Home > Web Front-end > JS Tutorial > body text

Teach you how to use node.js to create a child process

小云云
Release: 2017-12-11 09:35:58
Original
1558 people have browsed it

There is a lot of knowledge related to multi-process. It was also mentioned when writing the Process module, but I found that it is not too difficult to understand. This article mainly introduces you to the relevant information on how to create a child process using node.js. Friends who need it can refer to it. I hope it can help you.

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 multi-processor machines. 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 + " 执行。" );
Copy after login


exec() method

Create a new node-childPro-exec.js file in the node-childProcess file, with the following code:


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);
 });
}
Copy after login


The terminal execution code results are as follows:


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


##spawn() method

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


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);
 });
}
Copy after login


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
Copy after login


##fork() methodCreate a new node-childPro-fork.js in the node-childProcess file, with the following code:

##
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);
 });
}
Copy after login


Terminal execution code results are as follows:

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



#About exec, spawn, fork

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

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


3. After the sub-thread starts executing, spawn begins to continuously return data from the sub-process to the main process (application scenarios such as "system monitoring" ”)


4.spawn does not support the callback function. It sends data to the main process through a stream, thus realizing data exchange between multiple processes


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


Have you learned it? Hurry up and give it a try.

Related recommendations:

PHP and Node.js

Node.js’s brief introduction to asynchronous flow control

Node.JS related knowledge

The above is the detailed content of Teach you how to use node.js to create a child process. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!