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

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

问题描述

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

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.

一个好的经验法则是每个内核一到两个节点进程,对于具有良好的内存时钟/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一个Node命令来使用spawn进行上述操作.但这是愚蠢的,因为fork做了一些事情来优化创建V8实例的过程.只需说清楚,最终生成就包含了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子进程-生成与生成之间的区别叉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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