Home > Article > Web Front-end > Teach you how to use node.js to create a child process
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 + " 执行。" );
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('node node-childPro.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); }); childProcess.on('exit', function (code) { console.log('子进程已退出,退出码 ' + code); }); }
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:
##spawn() method
Create a new node-childPro-spawn.js 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++) { var childProcess = child_process.spawn('node', ['node-childPro-spawn.js', i]); childProcess.stdout.on('data', function (data) { console.log('stdout: ' + data); }); childProcess.stderr.on('data', function (data) { console.log('stderr: ' + data); }); childProcess.on('close', function (code) { console.log('子进程已退出,退出码 '+code); }); }
G:\node\node-childProcess> node node-childPro-spawn.js stdout: 进程 0 执行。 子进程已退出,退出码 0 stdout: 进程 1 执行。 stdout: 进程 2 执行。 子进程已退出,退出码 0 子进程已退出,退出码 0
##fork() methodCreate a new node-childPro-fork.js 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++) { var childProcess = child_process.fork("node-childPro.js", [i]); childProcess.on('close', function (code) { console.log('子进程已退出,退出码 ' + code); }); }
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 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
Have you learned it? Hurry up and give it a try.
Related recommendations:
PHP and Node.jsNode.js’s brief introduction to asynchronous flow control
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!