An in-depth analysis of how to create child processes in Node.js
This article will take you to understand the subprocess in Node.js, and introduce the four methods of creating subprocesses in Node.js. I hope it will be helpful to everyone!
As we all know, Node.js is a single-threaded, asynchronous and non-blocking programming language. So how to make full use of the advantages of multi-core CPUs? This requires the child_process module to create a child process. In Node.js, there are four ways to create a child process:
exec
execFile
##spawn
fork
nodejs tutorial"]
The above four methods will returnChildProcess instance (inherited from
EventEmitter), which has three standard stdio streams:
child.stdin
child.stdout
- ##child.stderr
: Triggered when the child process ends. The parameters are the code error code and the signal interrupt signal.
: Triggered when the child process ends and the stdio stream is closed. The parameters are the same as the exit
event.
: Triggered when the parent process calls child.disconnect()
or the child process calls process.disconnect()
.
: Triggered when the child process cannot be created, cannot be killed, or fails to send a message to the child process.
: Triggered when the child process sends a message through process.send()
.
: Triggered when the child process is successfully created (this event was only added in Node.js v15.1). The
and execFile
methods also provide an additional callback function, which will be triggered when the child process terminates. Next, detailed analysis: exec
The exec method is used to execute the bash command, and its parameter is a command string. For example, to count the number of files in the current directory, the exec function is written as:
const { exec } = require("child_process") exec("find . -type f | wc -l", (err, stdout, stderr) => { if (err) return console.error(`exec error: ${err}`) console.log(`Number of files ${stdout}`) })
exec will create a new sub-process, cache its running results, and call the callback function after the run is completed.
You may have thought that the exec command is relatively dangerous. If you use the string provided by the user as a parameter of the exec function, you will face the risk of command line injection, for example:
find . -type f | wc -l; rm -rf /;
In addition , since exec will cache all output results in memory, when the data is relatively large, spawn will be a better choice.
execFile
The difference between execFile and exec is that it does not create a shell, but directly executes the command, so it will be more efficient, for example:
const { execFile } = require("child_process") const child = execFile("node", ["--version"], (error, stdout, stderr) => { if (error) throw error console.log(stdout) })
Since there is no creation Shell, the parameters of the program are passed in as an array, so it has high security.
spawn
The spawn function is similar to execFile. The shell is not opened by default, but the difference is that execFile caches the output of the command line and then passes the result into the callback function, while spawn uses a stream output, with streams, it is very convenient to connect input and output. For example, the typical
wc command: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>const child = spawn("wc")
process.stdin.pipe(child.stdin)
child.stdout.on("data", data => {
console.log(`child stdout:\n${data}`)
})</pre><div class="contentsignin">Copy after login</div></div>
At this time, the input will be obtained from the command line stdin. When When the user triggers the carriage return
, the command starts executing and the results are output from stdout.
wc [OPTION]... [FILE]...Copy after loginIf you enter the wc command on the terminal and press Enter, the count is from the keyboard. Enter the characters in the terminal, press the Enter key again, and then press
Ctrl Dto output the statistical results.
You can also combine complex commands through pipelines, such as counting the number of files in the current directory. In the Linux command line, it will be written like this:
find . -type f | wc -l
How to write it in Node.js Exactly the same as the command line:
const find = spawn("find", [".", "-type", "f"]) const wc = spawn("wc", ["-l"]) find.stdout.pipe(wc.stdin) wc.stdout.on("data", (data) => { console.log(`Number of files ${data}`) })
spawn has rich custom configurations, for example:
const child = spawn("find . -type f | wc -l", { stdio: "inherit", // 继承父进程的输入输出流 shell: true, // 开启命令行模式 cwd: "/Users/keliq/code", // 指定执行目录 env: { ANSWER: 42 }, // 指定环境变量(默认是 process.env) detached: true, // 作为独立进程存在 })
fork
The fork function is a variant of the spawn function, using the child created by fork A communication channel will be automatically created between the process and the parent process, and the send method will be mounted on the global object process of the child process. For example, the parent process parent.js code:
const { fork } = require("child_process") const forked = fork("./child.js") forked.on("message", msg => { console.log("Message from child", msg); }) forked.send({ hello: "world" })
The child process child.js code:
process.on("message", msg => { console.log("Message from parent:", msg) }) let counter = 0 setInterval(() => { process.send({ counter: counter++ }) }, 1000)
When calling
fork("child.js"), actually Just use node to execute the code in the file, which is equivalent to spawn('node', ['./child.js'])
. A typical application scenario of fork is as follows: If you use Node.js to create an http service, when the route is
, a time-consuming operation is performed. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>const http = require("http")
const server = http.createServer()
server.on("request", (req, res) => {
if (req.url === "/compute") {
const sum = longComputation()
return res.end(Sum is ${sum})
} else {
res.end("OK")
}
})
server.listen(3000);</pre><div class="contentsignin">Copy after login</div></div>
You can use the following code to simulate this time-consuming operation:
const longComputation = () => { let sum = 0; for (let i = 0; i < 1e9; i++) { sum += i } return sum }
那么在上线后,只要服务端收到了 compute
请求,由于 Node.js 是单线程的,耗时运算占用了 CPU,用户的其他请求都会阻塞在这里,表现出来的现象就是服务器无响应。
解决这个问题最简单的方法就是把耗时运算放到子进程中去处理,例如创建一个 compute.js
的文件,代码如下:
const longComputation = () => { let sum = 0; for (let i = 0; i < 1e9; i++) { sum += i; } return sum } process.on("message", msg => { const sum = longComputation() process.send(sum) })
再把服务端的代码稍作改造:
const http = require("http") const { fork } = require("child_process") const server = http.createServer() server.on("request", (req, res) => { if (req.url === "/compute") { const compute = fork("compute.js") compute.send("start") compute.on("message", sum => { res.end(Sum is ${sum}) }) } else { res.end("OK") } }) server.listen(3000)
这样的话,主线程就不会阻塞,而是继续处理其他的请求,当耗时运算的结果返回后,再做出响应。其实更简单的处理方式是利用 cluster 模块,限于篇幅原因,后面再展开讲。
总结
掌握了上面四种创建子进程的方法之后,总结了以下三条规律:
- 创建 node 子进程用 fork,因为自带通道方便通信。
- 创建非 node 子进程用 execFile 或 spawn。如果输出内容较少用 execFile,会缓存结果并传给回调方便处理;如果输出内容多用 spawn,使用流的方式不会占用大量内存。
- 执行复杂的、固定的终端命令用 exec,写起来更方便。但一定要记住 exec 会创建 shell,效率不如 execFile 和 spawn,且存在命令行注入的风险。
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of An in-depth analysis of how to create child processes in Node.js. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

How does Node.js do GC (garbage collection)? The following article will take you through it.

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".
