node.js 子进程 - spawn & 之间的区别叉 [英] node.js child process - difference between spawn & fork

查看:29
本文介绍了node.js 子进程 - spawn & 之间的区别叉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

推荐答案

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

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.

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

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.

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

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.

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

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.

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

这篇关于node.js 子进程 - spawn & 之间的区别叉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆