Table of Contents
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:
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:
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
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:
总结
Home Web Front-end JS Tutorial An in-depth analysis of how to create child processes in Node.js

An in-depth analysis of how to create child processes in Node.js

Oct 12, 2021 am 10:05 AM
node.js child process

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!

An in-depth analysis of how to create child processes in Node.js

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

[Recommended study: "

nodejs tutorial"]

The above four methods will return

ChildProcess instance (inherited from EventEmitter), which has three standard stdio streams:

  • child.stdin

  • child.stdout

  • ##child.stderr

  • The events that can be registered and monitored during the life cycle of the child process are:

exit

: Triggered when the child process ends. The parameters are the code error code and the signal interrupt signal.

close

: Triggered when the child process ends and the stdio stream is closed. The parameters are the same as the exit event.

disconnect

: Triggered when the parent process calls child.disconnect() or the child process calls process.disconnect().

error

: Triggered when the child process cannot be created, cannot be killed, or fails to send a message to the child process.

message

: Triggered when the child process sends a message through process.send().

spawn

: Triggered when the child process is successfully created (this event was only added in Node.js v15.1). The

exec

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

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

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

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(&quot;wc&quot;) process.stdin.pipe(child.stdin) child.stdout.on(&quot;data&quot;, data =&gt; { 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

ctrl D

, the command starts executing and the results are output from stdout.

wc is the abbreviation of Word Count, which is used to count the number of words. The syntax is:
wc [OPTION]... [FILE]...
Copy after login

If 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 D

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

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

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, // 作为独立进程存在
})
Copy after login

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

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

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

compute

, 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(&quot;http&quot;) const server = http.createServer() server.on(&quot;request&quot;, (req, res) =&gt; { if (req.url === &quot;/compute&quot;) { const sum = longComputation() return res.end(Sum is ${sum}) } else { res.end(&quot;OK&quot;) } }) 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
}
Copy after login

那么在上线后,只要服务端收到了 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)
})
Copy after login

再把服务端的代码稍作改造:

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

这样的话,主线程就不会阻塞,而是继续处理其他的请求,当耗时运算的结果返回后,再做出响应。其实更简单的处理方式是利用 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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

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

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

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!

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

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.

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

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.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

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!

Let's talk about the GC (garbage collection) mechanism in Node.js Let's talk about the GC (garbage collection) mechanism in Node.js Nov 29, 2022 pm 08:44 PM

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

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

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!

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

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".

See all articles