Home > Web Front-end > JS Tutorial > body text

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

青灯夜游
Release: 2021-10-12 10:05:48
forward
2971 people have browsed it

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:js;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:js;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!

Related labels:
source:juejin.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template