Home  >  Article  >  Web Front-end  >  About the child_process module in node (detailed tutorial)

About the child_process module in node (detailed tutorial)

亚连
亚连Original
2018-06-09 16:23:511693browse

This article mainly introduces the detailed study notes of the node child_process module. Now I will share them with you and give them a reference.

NodeJs is a single-process language and cannot create multiple threads for concurrent execution like Java. Of course, in most cases, NodeJs does not require concurrent execution because it is event-driven and never blocks. However, a single process also has a problem that it cannot fully utilize the multi-core mechanism of the CPU. According to previous experience, multiple processes can be created to fully utilize the multi-core CPU, and Node uses the child_process module to create and complete multi-process operations.

The child_process module gives node the ability to create child processes at will. The official document of node gives four methods for the child_proces module. Mapping to the operating system actually creates child processes. But for developers, the APIs of these methods are a bit different.

child_process.exec(command[, options][, callback]) starts the child process to execute the shell command. You can obtain the script shell through the callback parameter. Execution result

child_process.execfile(file[, args][, options][, callback]) Different from the exec type, it executes not a shell command but an executable file

child_process.spawn(command[, args][, options]) only executes a shell command and does not need to obtain the execution results

child_process.fork(modulePath[, args][, options]) can be executed with node .js file, there is no need to obtain execution results. The child process coming out of the fork must be a node process

When exec() and execfile() are created, you can specify the timeout attribute to set the timeout. Once it times out, it will be killed

If you use execfile( ) to execute the executable file, then the header must be #!/usr/bin/env node

Inter-process communication

The communication between node and the child process is This is done using the IPC pipe mechanism. If the child process is also a node process (using fork), you can listen to the message event and use send() to communicate.

main.js

var cp = require('child_process');
//只有使用fork才可以使用message事件和send()方法
var n = cp.fork('./child.js');
n.on('message',function(m){
 console.log(m);
})

n.send({"message":"hello"});

child.js

var cp = require('child_process');
process.on('message',function(m){
 console.log(m);
})
process.send({"message":"hello I am child"})

An IPC channel will be created between the parent and child processes, and the message event and send() will use the IPC channel to communicate.

Handle passing

After learning how to create a child process, we create an HTTP service and start multiple processes to jointly make full use of the multi-core CPU.

worker.js

var http = require('http');
http.createServer(function(req,res){
 res.end('Hello,World');
 //监听随机端口
}).listen(Math.round((1+Math.random())*1000),'127.0.0.1');

main.js

var fork = require('child_process').fork;
var cpus = require('os').cpus();
for(var i=0;i<cpus.length;i++){
 fork(&#39;./worker.js&#39;);
}

The above code will create a corresponding number of fork processes according to the number of your cpu cores, and each process listens to a random port to provide HTTP services.

The above completes a typical Master-Worker master-slave replication model. It is used for parallel processing of business in distributed applications and has good shrinkage and stability. It should be noted here that forking a process is expensive, and the node single-process event driver has good performance. The multiple fork processes in this example are to make full use of the CPU core, not to solve the concurrency problem.

One bad thing about the above example is that it occupies too many ports, so can all of the multiple child processes be used? Using the same port to provide http services to the outside world only uses this port. Try changing the above random port number to 8080. When starting, you will find that the following exception is thrown.

events.js:72
  throw er;//Unhandled &#39;error&#39; event
Error:listen EADDRINUSE
XXXX

Throws an exception that the port is occupied, which means that only one worker.js can listen to port 8080, and the rest will throw exceptions.

If you want to solve the problem of providing a port to the outside world, you can refer to the nginx reverse proxy method. For the Master process, port 80 is used to provide services to the outside world, while for the fork process, a random port is used. When the Master process receives the request, it forwards it to the fork process.

For the proxy mode just mentioned, since the process each When a connection is received, one file descriptor will be used. Therefore, in the proxy mode, the client connects to the proxy process, and the proxy process then connects to the fork process, which will use two file descriptors. The number of file descriptors in the OS is limited. In order to solve the problem For this problem, node introduces the function of sending handles between processes.

In the IPC process communication API of node, the second parameter of send(message, [sendHandle]) is the handle.

A handle is a reference that identifies a resource, and it contains the file descriptor pointing to the object. The handle can be used to describe a socket object, a UDP socket, and a pipe. Sending a handle to the worker process by the main process means that when the main process receives the client's socket request, it will directly send the socket to the worker process without further contact. When the worker process establishes a socket connection, the waste of file descriptors can be solved. Let’s look at the sample code:

main.js

var cp = require(&#39;child_process&#39;);
var child = cp.fork(&#39;./child.js&#39;);
var server = require(&#39;net&#39;).createServer();
//监听客户端的连接
server.on(&#39;connection&#39;,function(socket){
 socket.end(&#39;handled by parent&#39;);
});
//启动监听8080端口
server.listen(8080,function(){
//给子进程发送TCP服务器(句柄)
 child.send(&#39;server&#39;,server);
});

child.js

process.on(&#39;message&#39;,function(m,server){
 if(m===&#39;server&#39;){
 server.on(&#39;connection&#39;,function(socket){
  socket.end(&#39;handle by child&#39;);
 });
 }
});

You can test it using telnet or curl:

wang@wang ~ /code/nodeStudy $ curl 192.168.10.104:8080
handled by parent
wang@wang ~/code/nodeStudy $ curl 192.168.10.104:8080
handle by child
wang@wang ~/code /nodeStudy $ curl 192.168.10.104:8080
handled by parent
wang@wang ~/code/nodeStudy $ curl 192.168.10.104:8080
handled by parent

The test result is every time For client connections, they may be handled by the parent process or by the child process. Now we try to only provide http services, and in order to make the parent process more lightweight, we only let the parent process pass the handle to the child process without doing request processing:

main.js

var cp = require(&#39;child_process&#39;);
var child1 = cp.fork(&#39;./child.js&#39;);
var child2 = cp.fork(&#39;./child.js&#39;);
var child3 = cp.fork(&#39;./child.js&#39;);
var child4 = cp.fork(&#39;./child.js&#39;);
var server = require(&#39;net&#39;).createServer();
//父进程将接收到的请求分发给子进程
server.listen(8080,function(){
 child1.send(&#39;server&#39;,server);
 child2.send(&#39;server&#39;,server);
 child3.send(&#39;server&#39;,server);
 child4.send(&#39;server&#39;,server);
 //发送完句柄后关闭监听
 server.close();
});

child. js

var http = require(&#39;http&#39;);
var serverInChild = http.createServer(function(req,res){
 res.end(&#39;I am child.Id:&#39;+process.pid);
});
//子进程收到父进程传递的句柄(即客户端与服务器的socket连接对象)
process.on(&#39;message&#39;,function(m,serverInParent){
 if(m===&#39;server&#39;){
 //处理与客户端的连接
 serverInParent.on(&#39;connection&#39;,function(socket){
  //交给http服务来处理
  serverInChild.emit(&#39;connection&#39;,socket);
 });
 }
});

When running the above code, checking the 8080 port occupancy will have the following results:

wang@wang ~/code/nodeStudy $ lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 5120 wang 11u IPv6 44561 0t0 TCP *:http-alt (LISTEN)
node 5126 wang 11u IPv6 44561 0t0 TCP *:http-alt (LISTEN)
node 5127 wang 11u IPv6 44561 0t0 TCP *:http-alt (LISTEN)
node 5133 wang 11u IPv6 44561 0t0 TCP * :http-alt (LISTEN)

Run curl to view the results:

wang@wang ~/code/nodeStudy $ curl 192.168.10.104:8080
I am child.Id:5127
wang@wang ~/code/nodeStudy $ curl 192.168.10.104:8080
I am child.Id:5133
wang@wang ~/code/nodeStudy $ curl 192.168.10.104:8080
I am child.Id:5120
wang@wang ~/code/nodeStudy $ curl 192.168.10.104:8080
I am child.Id:5126
wang@wang ~/code/nodeStudy $ curl 192.168.10.104: 8080
I am child.Id:5133
wang@wang ~/code/nodeStudy $ curl 192.168.10.104:8080
I am child.Id:5126

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

Related articles:

Detailed explanation of introducing elementUI components into the vue project

How to implement navigation with ElementUI in vue-router

How to implement page jump in vue and return to the initial position of the original page

How to set the title method of each page using vue-router

How to solve the problem of page flashing when Vue.js displays data

The above is the detailed content of About the child_process module in node (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