node.js 子进程 - spawn 和 fork 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17861362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:07:27  来源:igfitidea点击:

node.js child process - difference between spawn & fork

node.jsprocessparent-child

提问by Hitesh

This might seem like a basic question, but I could not find any documentation :

这似乎是一个基本问题,但我找不到任何文档:

What is the difference between forking & spawning a node.js process? I have read that forking is a special case of spawning, but what are the different use cases / repecussions for using each of them?

分叉和生成 node.js 进程有什么区别?我已经读过分叉是生成的一种特殊情况,但是使用它们中的每一个都有哪些不同的用例/反响?

回答by ChrisCM

Spawn is a command designed to run system commands. When you run spawn, you send it a system command that will be run on its own process, but does not execute any further code within your node process. You can add listeners for the process you have spawned, to allow your code interact with the spawned process, but no new V8 instance is created(unless of course your command is another Node command, but in this case you should use fork!) and only one copy of your node module is active on the processor.

Spawn 是一个旨在运行系统命令的命令。当您运行 spawn 时,您向它发送了一个系统命令,该命令将在其自己的进程上运行,但不会在您的节点进程中执行任何进一步的代码。您可以为您生成的进程添加侦听器,以允许您的代码与生成的进程交互,但不会创建新的 V8 实例(当然除非您的命令是另一个 Node 命令,但在这种情况下您应该使用 fork!)和处理器上只有一个节点模块的副本处于活动状态。

Fork is a special instance of spawn, that runs a fresh instance of the V8 engine. Meaning, you can essentially create multiple workers, running on the exact same Node code base, or perhaps a different module for a specific task. This is most useful for creating a worker pool. While node's async event model allows a single core of a machine to be used fairly efficiently, it doesn't allow a node process to make use of multi core machines. Easiest way to accomplish this is to run multiple copies of the same program, on a single processor.

Fork 是 spawn 的一个特殊实例,它运行 V8 引擎的一个新实例。这意味着,您基本上可以创建多个工作程序,运行在完全相同的 Node 代码库上,或者可能是针对特定任务的不同模块。这对于创建工作池最有用。虽然节点的异步事件模型允许相当有效地使用机器的单核,但它不允许节点进程使用多核机器。实现这一点的最简单方法是在单个处理器上运行同一程序的多个副本。

A good rule of thumb is one to two node processes per core, perhaps more for machines with a good ram clock/cpu clock ratio, or for node processes heavy on I/O and light on CPU work, to minimize the down time the event loop is waiting for new events. However, the latter suggestion is a micro-optimization, and would need careful benchmarking to ensure your situation suits the need for many processes/core. You can actually decrease performance by spawning too many workers for your machine/scenario.

一个好的经验法则是每个内核一到两个节点进程,对于具有良好 ram 时钟/cpu 时钟比率的机器,或者对于 I/O 重、CPU 工作轻的节点进程,可能更多,以最大限度地减少事件的停机时间循环正在等待新事件。但是,后一个建议是微优化,需要仔细的基准测试以确保您的情况适合许多进程/核心的需要。您实际上可以通过为您的机器/场景产生过多的工人来降低性能。

Ultimately you could use spawn in a way that did the above, by sending spawn a Node command. But this would be silly, because fork does some things to optimize the process of creating V8 instances. Just making it clear, that ultimately spawn encompasses fork. Fork is just optimal for this particular, and very useful, use case.

最终,您可以通过发送 spawn 节点命令以上述方式使用 spawn。但这很愚蠢,因为 fork 做了一些事情来优化创建 V8 实例的过程。只是说清楚,最终 spawn 包含 fork。Fork 最适合这个特定且非常有用的用例。

http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

回答by vijay

TLDR

TLDR

Spawn

When a spawnis created -It creates a streaming interfacebetween parent and child process.

产卵创建-它创建了一个流媒体接口,父母和孩子之间的过程。

streaming interface means- buffering data in binary format in ONE TIME

流接口意味着- 以二进制格式缓冲数据ONE TIME

Fork

When a forkis created -It creates a communication channelbetween parent and child process

创建-它创建了一个通信信道的父进程和子进程之间

communication channel means- messaging

沟通渠道手段- 消息传递

Difference

Well both look kind of doing same data transfer, Except below difference

好吧,两者看起来都在做相同的数据传输,除了下面的区别

spawnwill be useful when you want to do continuous data buffer in binary/encoding format, Eg - Transfer 1gb video file,image,log files in ONE TIME

当您想以二进制/编码格式进行连续数据缓冲区时,spawn将很有用,例如 - 传输 1gb 视频文件、图像、日志文件ONE TIME

forkwill be useful when you want to do messagingEg - JSONor XMLdata messaging

当您想要进行消息传递时,fork会很有用, 例如 -JSONXML数据消息传递

Conslusion

spawnshould be used for streamingbig data/files/images FROM spawnprocess TO parentprocess

spawn应该用于从spawn进程到进程的 大数据/文件/图像

forkshould be used for doing Json/Xml messaging .

fork应该用于执行 Json/Xml 消息传递。

  • Egsuppose 10 fork process are created from parent.
  • and each process performs some operation
  • and each process on completing operation will send message to parent 'process no.4 done','process no.8 done'
  • 例如,假设 10 个 fork 进程是从父进程创建的。
  • 每个进程执行一些操作
  • 每个完成操作的进程都会发送消息给父进程'进程号完成','进程号8完成'

回答by Igor Litvinovich

  • spawn? child_process.spawnlaunches a new process with a given command.
  • fork? The child_process.forkmethod is a special case of the spawn()to create child processes.
  • 产卵child_process.spawn使用给定命令启动一个新进程。
  • 叉子?该child_process.fork方法是一个特殊的情况下产卵()创建子进程。

The spawn() Method

spawn() 方法

child_process.spawn method launches a new process with a given command. It has the following signature ?

child_process.spawn 方法使用给定的命令启动一个新进程。它有以下签名?

child_process.spawn(command[, args][, options])

Read more about options

阅读有关选项的更多信息

The spawn() method returns streams (stdout &stderr) and it should be used when the process returns a volume amount of data. spawn() starts receiving the response as soon as the process starts executing.

spawn() 方法返回流(stdout &stderr),当进程返回大量数据时应该使用它。一旦进程开始执行,spawn() 就开始接收响应。

The fork() Method

fork() 方法

child_process.forkmethod is a special case of spawn()to create Node processes. It has the following signature ?

child_process.fork方法是spawn()创建 Node 进程的特例。它有以下签名?

 child_process.fork(modulePath[, args][, options])

The fork method returns an object with a built-in communication channel in addition to having all the methods in a normal ChildProcess instance.

除了在普通 ChildProcess 实例中拥有所有方法之外,fork 方法还返回一个具有内置通信通道的对象。