<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=Node.js%EF%BC%9A%E5%A4%9A%E8%BF%9B%E7%A8%8B</id>
	<title>Node.js：多进程 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=Node.js%EF%BC%9A%E5%A4%9A%E8%BF%9B%E7%A8%8B"/>
	<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=Node.js%EF%BC%9A%E5%A4%9A%E8%BF%9B%E7%A8%8B&amp;action=history"/>
	<updated>2026-04-26T05:48:28Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=Node.js%EF%BC%9A%E5%A4%9A%E8%BF%9B%E7%A8%8B&amp;diff=6458&amp;oldid=prev</id>
		<title>2023年3月31日 (五) 13:21 Eijux</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=Node.js%EF%BC%9A%E5%A4%9A%E8%BF%9B%E7%A8%8B&amp;diff=6458&amp;oldid=prev"/>
		<updated>2023-03-31T13:21:38Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2023年3月31日 (五) 21:21的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;第1行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第1行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:Node.&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;js&lt;/del&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:Node.&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;js教程&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 关于 ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 关于 ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=Node.js%EF%BC%9A%E5%A4%9A%E8%BF%9B%E7%A8%8B&amp;diff=4162&amp;oldid=prev</id>
		<title>Eijux：​建立内容为“category:Node.js  == 关于 == Node.js 是以单线程的模式运行的，但它使用的是事件驱动来处理并发，这样有助于我们在多核 cpu…”的新页面</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=Node.js%EF%BC%9A%E5%A4%9A%E8%BF%9B%E7%A8%8B&amp;diff=4162&amp;oldid=prev"/>
		<updated>2021-05-31T15:35:31Z</updated>

		<summary type="html">&lt;p&gt;建立内容为“&lt;a href=&quot;/%E5%88%86%E7%B1%BB:Node.js&quot; title=&quot;分类:Node.js&quot;&gt;category:Node.js&lt;/a&gt;  == 关于 == Node.js 是以单线程的模式运行的，但它使用的是事件驱动来处理并发，这样有助于我们在多核 cpu…”的新页面&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[category:Node.js]]&lt;br /&gt;
&lt;br /&gt;
== 关于 ==&lt;br /&gt;
Node.js 是以单线程的模式运行的，但它使用的是事件驱动来处理并发，这样有助于我们在多核 cpu 的系统上创建多个子进程，从而提高性能。&lt;br /&gt;
&lt;br /&gt;
每个子进程总是带有三个流对象：'''child.stdin'''、'''child.stdout''' 和'''child.stderr'''。他们可能会共享父进程的 stdio 流，或者也可以是独立的被导流的流对象。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Node 提供了 '''child_process''' 模块来创建子进程，方法有：&lt;br /&gt;
# '''exec'''：“child_process.exec”使用子进程执行命令，缓存子进程的输出，并将子进程的输出以回调函数参数的形式返回。&lt;br /&gt;
# '''spawn'''：“child_process.spawn”使用指定的命令行参数创建新进程。&lt;br /&gt;
# '''fork'''：“child_process.fork”是 spawn() 的特殊形式，用于在子进程中运行的模块。如：“fork('./son.js')”相当于“spawn('node', ['./son.js'])” 。&lt;br /&gt;
* 与 spawn 方法不同的是，fork 会在父进程与子进程之间，建立一个'''通信管道'''，用于进程之间的通信。&lt;br /&gt;
&lt;br /&gt;
== '''exec'''方法 ==&lt;br /&gt;
'''&amp;lt;code&amp;gt;child_process.exec&amp;lt;/code&amp;gt;''' '''使用子进程执行命令，缓存子进程的输出，并将子进程的输出以回调函数参数的形式返回'''。&lt;br /&gt;
&lt;br /&gt;
语法：&lt;br /&gt;
'''&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
child_process.exec(command[, options], callback)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;'''&lt;br /&gt;
* exec() 方法返回最大的缓冲区，并等待进程结束，一次性返回缓冲区的内容。&lt;br /&gt;
&lt;br /&gt;
参数：&lt;br /&gt;
* command：（String）将要运行的命令，参数使用空格隔开；&lt;br /&gt;
* options：（Object）：&lt;br /&gt;
** cwd：（String）子进程的当前工作目录；&lt;br /&gt;
** env：（Object）环境变量键值对；&lt;br /&gt;
** encoding：（String）字符编码；（默认： 'utf8'）&lt;br /&gt;
** shell：（String）将要执行命令的 Shell；（默认：在 UNIX 中为 /bin/sh， 在 Windows 中为 cmd.exe）&lt;br /&gt;
** timeout：（Number）超时时间；（默认： 0）&lt;br /&gt;
** maxBuffer：（Number）在 stdout 或 stderr 中允许存在的最大缓冲（二进制），如果超出那么子进程将会被杀死；（默认: 200*1024）&lt;br /&gt;
** killSignal：（String）结束信号；（默认：'SIGTERM'）&lt;br /&gt;
** uid：（Number）设置用户进程的 ID；&lt;br /&gt;
** gid：（Number）设置进程组的 ID；&lt;br /&gt;
* callback ：回调函数，包含三个参数 '''error''', '''stdout''' 和 '''stderr'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
# 创建 support.js：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
console.log(&amp;quot;进程 &amp;quot; + process.argv[2] + &amp;quot; 执行。&amp;quot; );&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 创建 master.js：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot; highlight=&amp;quot;2,5&amp;quot;&amp;gt;&lt;br /&gt;
const fs = require('fs');&lt;br /&gt;
const child_process = require('child_process');&lt;br /&gt;
 &lt;br /&gt;
for(var i=0; i&amp;lt;3; i++) {&lt;br /&gt;
    var workerProcess = child_process.exec('node support.js ' + i, function (error, stdout, stderr) {&lt;br /&gt;
        if (error) {&lt;br /&gt;
            console.log(error.stack);&lt;br /&gt;
            console.log('Error code: ' + error.code);&lt;br /&gt;
            console.log('Signal received: ' + error.signal);&lt;br /&gt;
        }&lt;br /&gt;
        console.log('stdout: ' + stdout);&lt;br /&gt;
        console.log('stderr: ' + stderr);&lt;br /&gt;
    });&lt;br /&gt;
 &lt;br /&gt;
    workerProcess.on('exit', function (code) {&lt;br /&gt;
        console.log('子进程已退出，退出码 ' + code);&lt;br /&gt;
    });&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 执行以上代码，输出结果为：【？？？】&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
$ node master.js &lt;br /&gt;
子进程已退出，退出码 0&lt;br /&gt;
stdout: 进程 1 执行。&lt;br /&gt;
&lt;br /&gt;
stderr: &lt;br /&gt;
子进程已退出，退出码 0&lt;br /&gt;
stdout: 进程 0 执行。&lt;br /&gt;
&lt;br /&gt;
stderr: &lt;br /&gt;
子进程已退出，退出码 0&lt;br /&gt;
stdout: 进程 2 执行。&lt;br /&gt;
&lt;br /&gt;
stderr:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== '''spawn'''方法 ==&lt;br /&gt;
'''&amp;lt;code&amp;gt;child_process.spawn&amp;lt;/code&amp;gt;''' '''使用指定的命令行参数创建新进程'''。&lt;br /&gt;
&lt;br /&gt;
语法：&lt;br /&gt;
'''&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
child_process.spawn(command[, args][, options])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;'''&lt;br /&gt;
* spawn() 方法返回流 (stdout &amp;amp; stderr)，在进程返回大量数据时使用。&lt;br /&gt;
* 进程一旦开始执行时 spawn() 就开始接收响应。&lt;br /&gt;
&lt;br /&gt;
参数：&lt;br /&gt;
* command：（String）将要运行的命令；&lt;br /&gt;
* args：（Array）字符串参数；&lt;br /&gt;
* options：（Object）：&lt;br /&gt;
** cwd：（String）子进程的当前工作目录；&lt;br /&gt;
** env：（Object）环境变量键值对；&lt;br /&gt;
** stdio：（Array|String）子进程的 stdio 配置；&lt;br /&gt;
** detached：（Boolean）这个子进程将会变成进程组的领导；&lt;br /&gt;
** uid：（Number）设置用户进程的 ID；&lt;br /&gt;
** gid：（Number）设置进程组的 ID；&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
# 创建 support.js：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
console.log(&amp;quot;进程 &amp;quot; + process.argv[2] + &amp;quot; 执行。&amp;quot; );&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 创建 master.js：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot; highlight=&amp;quot;2,5&amp;quot;&amp;gt;&lt;br /&gt;
const fs = require('fs');&lt;br /&gt;
const child_process = require('child_process');&lt;br /&gt;
 &lt;br /&gt;
for(var i=0; i&amp;lt;3; i++) {&lt;br /&gt;
   var workerProcess = child_process.spawn('node', ['support.js', i]);&lt;br /&gt;
 &lt;br /&gt;
   workerProcess.stdout.on('data', function (data) {&lt;br /&gt;
      console.log('stdout: ' + data);&lt;br /&gt;
   });&lt;br /&gt;
 &lt;br /&gt;
   workerProcess.stderr.on('data', function (data) {&lt;br /&gt;
      console.log('stderr: ' + data);&lt;br /&gt;
   });&lt;br /&gt;
 &lt;br /&gt;
   workerProcess.on('close', function (code) {&lt;br /&gt;
      console.log('子进程已退出，退出码 ' + code);&lt;br /&gt;
   });&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 执行以上代码，输出结果为：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
$ node master.js &lt;br /&gt;
stdout: 进程 0 执行。&lt;br /&gt;
&lt;br /&gt;
子进程已退出，退出码 0&lt;br /&gt;
stdout: 进程 1 执行。&lt;br /&gt;
&lt;br /&gt;
子进程已退出，退出码 0&lt;br /&gt;
stdout: 进程 2 执行。&lt;br /&gt;
&lt;br /&gt;
子进程已退出，退出码 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== '''fork'''方法 ==&lt;br /&gt;
'''&amp;lt;code&amp;gt;child_process.fork&amp;lt;/code&amp;gt;''' 是 spawn() 方法的特殊形式，用于'''创建进程'''。&lt;br /&gt;
&lt;br /&gt;
语法：&lt;br /&gt;
'''&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
child_process.fork(modulePath[, args][, options])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;'''&lt;br /&gt;
* 返回的对象除了拥有 ChildProcess 实例的所有方法，还有一个内建的'''通信信道'''。&lt;br /&gt;
&lt;br /&gt;
参数：&lt;br /&gt;
* modulePath：（String）将要在子进程中运行的模块；&lt;br /&gt;
* args：（Array）字符串参数数组；&lt;br /&gt;
* options：（Object）：&lt;br /&gt;
** cwd：（String）子进程的当前工作目录；&lt;br /&gt;
** env：（Object）环境变量键值对；&lt;br /&gt;
** execPath：（String）创建子进程的可执行文件；&lt;br /&gt;
** execArgv：（Array）子进程的可执行文件的字符串参数数组；（默认： process.execArgv）&lt;br /&gt;
** silent：（Boolean）如果为 true，子进程的 stdin，stdout 和 stderr 将会被关联至父进程，否则，它们将会从父进程中继承；（默认为：false）&lt;br /&gt;
** uid：（Number）设置用户进程的 ID；&lt;br /&gt;
** gid：（Number）设置进程组的 ID；&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
# 创建 support.js：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
console.log(&amp;quot;进程 &amp;quot; + process.argv[2] + &amp;quot; 执行。&amp;quot; );&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 创建 master.js：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot; highlight=&amp;quot;2,5&amp;quot;&amp;gt;&lt;br /&gt;
const fs = require('fs');&lt;br /&gt;
const child_process = require('child_process');&lt;br /&gt;
 &lt;br /&gt;
for(var i=0; i&amp;lt;3; i++) {&lt;br /&gt;
   var worker_process = child_process.fork(&amp;quot;support.js&amp;quot;, [i]);    &lt;br /&gt;
 &lt;br /&gt;
   worker_process.on('close', function (code) {&lt;br /&gt;
      console.log('子进程已退出，退出码 ' + code);&lt;br /&gt;
   });&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 执行以上代码，输出结果为：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
$ node master.js &lt;br /&gt;
进程 0 执行。&lt;br /&gt;
子进程已退出，退出码 0&lt;br /&gt;
进程 1 执行。&lt;br /&gt;
子进程已退出，退出码 0&lt;br /&gt;
进程 2 执行。&lt;br /&gt;
子进程已退出，退出码 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
</feed>