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('./worker.js');
}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 'error' 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('child_process');
var child = cp.fork('./child.js');
var server = require('net').createServer();
//监听客户端的连接
server.on('connection',function(socket){
socket.end('handled by parent');
});
//启动监听8080端口
server.listen(8080,function(){
//给子进程发送TCP服务器(句柄)
child.send('server',server);
});child.js
process.on('message',function(m,server){
if(m==='server'){
server.on('connection',function(socket){
socket.end('handle by child');
});
}
});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('child_process');
var child1 = cp.fork('./child.js');
var child2 = cp.fork('./child.js');
var child3 = cp.fork('./child.js');
var child4 = cp.fork('./child.js');
var server = require('net').createServer();
//父进程将接收到的请求分发给子进程
server.listen(8080,function(){
child1.send('server',server);
child2.send('server',server);
child3.send('server',server);
child4.send('server',server);
//发送完句柄后关闭监听
server.close();
});child. js
var http = require('http');
var serverInChild = http.createServer(function(req,res){
res.end('I am child.Id:'+process.pid);
});
//子进程收到父进程传递的句柄(即客户端与服务器的socket连接对象)
process.on('message',function(m,serverInParent){
if(m==='server'){
//处理与客户端的连接
serverInParent.on('connection',function(socket){
//交给http服务来处理
serverInChild.emit('connection',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!
JavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AMThe main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.
Understanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AMUnderstanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.
Python vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AMPython is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
Python vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AMPython and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.
From C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AMThe shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
JavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AMDifferent JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.
Beyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AMJavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.
Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AMI built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Mac version
God-level code editing software (SublimeText3)






