nodejs架构_Nodejs的完美架构:技术上如何工作?

nodejs架构

The main goal of this blog is to explain the “Architecture of Nodejs” and to know how the Nodejs works behind the scenes,

该博客的主要目的是解释“ Nodejs的体系结构 ”,并了解Nodejs在幕后的工作方式,

Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, and including Nodejs follows multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

通常,大多数服务器端语言(如PHP,ASP.NET,Ruby和包括Nodejs)都遵循多线程体系结构。 这意味着对于每个客户端请求,都会启动一个新线程甚至一个新进程。

In Nodejs, all those requests from the clients are handled in a single-thread using shared resources concurrently as It follows the “Single-Threaded Event Loop Model”.

在Node.js中,所有来自客户端的请求都在单线程中使用共享资源并发处理,因为它遵循“单线程事件循环模型”。

NODEJS的体系结构 (ARCHITECTURE OF NODEJS)

什么是EVENT-LOOP? (What Is EVENT-LOOP?)

Event-Loop programming is a flow control in an application-defined by events. The basic principle of Nodejs’s event-driven loop is implementing a central mechanism that hears for events and calls the callback function once an event is turning up.

事件循环编程是应用程序中由事件定义的流控制。 Nodejs事件驱动循环的基本原理是实现一种中心机制,该机制侦听事件并在事件发生时调用回调函数。

Nodejs is an event-loop that implements a run-time environment model to achieve non-blocking asynchronous behavior runs on Google Chrome’s V8 engine.

Nodejs是一个事件循环,它实现了运行时环境模型,以实现在Google Chrome的V8引擎上运行的非阻塞异步行为

An event loop that is run by Node thread goes active until a task gets ends. Thus, it activates the secondary event which signals the event-listener function to execute the task on time.

由Node线程运行的事件循环将变为活动状态,直到任务结束。 因此,它激活了辅助事件,该事件向事件侦听器功能发出信号以按时执行任务。

As soon as Nodejs begins to work, it initializes an event loop. And it processes the given input script(i.e)variable initiation and a function declaration. This eventually makes asynchronous API calls and schedule timers, then begins processing the event loop.

Nodejs一旦开始工作,就会初始化一个事件循环。 并且它处理给定的输入脚本(即变量初始化和函数声明)。 最终,这将进行异步API调用并安排计时器,然后开始处理事件循环。

Still, having a question on “How event-loop works”?? Here the sample Pseudo-code that clearly explains the model,

还是有一个关于“事件循环如何工作”的问题吗? 这里的示例伪代码清楚地说明了模型,

public class EventLoop {while(true){         if(Event Queue receives a JavaScript Function Call){         ClientRequest request = EventQueue.getClientRequest();                            If(request requires BlockingIO or takes more computation time)                                    Assign request to Thread T1                            Else                                  Process and Prepare response                  }            }}

Some other integral parts of this architecture of Nodejs are:

Nodejs架构的其他一些不可或缺的部分是:

非阻塞I / O模型 (NON-BLOCKING I/O MODEL)

Node’s main thread doesn’t wait for anything literally, not even for an asynchronous process to complete. Such an integral part of Nodejs architecture is known to be Non-blocking I/O model.

Node的主线程实际上不会等待任何事情,甚至也不会等待异步过程完成。 众所周知,Nodejs架构的这种不可分割的部分是非阻塞I / O模型。

In simple, main threads in Node executes a background thread that doesn’t wait for any asynchronous task to get terminated. And then the main thread is free to allow other requests for the execution process.

简单来说,Node中的主线程执行一个后台线程,该线程不等待任何异步任务终止。 然后主线程可以自由地允许其他请求执行过程。

Node’s main thread is continuously switching between different requests to execute its synchronous Part.

节点的主线程在不同的请求之间连续切换以执行其同步部分。

基于事件的架构 (EVENT-BASED ARCHITECTURE)

The Background thread in Nodejs architecture uses an Event-based approach to report the main thread. Each asynchronous task consists of some Callback function associated with it.

Nodejs体系结构中的Background线程使用基于事件的方法来报告主线程。 每个异步任务都包含一些与其关联的回调函数。

Once the asynchronous task is complete, the background thread raises an event. All this to report the main thread about the completion of the asynchronous task.

异步任务完成后,后台线程将引发一个事件。 所有这些报告了有关异步任务完成的主线程。

The main thread will be busy processing other requests, meanwhile, the request waits on the main thread callback request for the execution.

主线程将忙于处理其他请求,与此同时,该请求等待主线程回调请求执行。

The various phases of the event-loop are illustrated below for a better understanding of Nodejs architecture:

为了更好地理解Nodejs架构,下面说明了事件循环的各个阶段:

关于Nodejs架构的一些常见问题和误解: (Some FAQs And Misconception About The Nodejs Architecture:)

Nodejs是完全单线程的吗? (Is Nodejs Completely Single-Threaded?)

This is a very common misconception about the Nodejs architecture. Node runs on a single thread, but some of its functions in the Nodejs standard library doesn’t run their logic outside the Nodejs single thread. This practice carried out to maintain the programs’ speed and performance.

这是对Nodejs架构的非常普遍的误解。 Node在单线程上运行,但是Nodejs标准库中的某些功能不在Nodejs单线程外运行其逻辑。 进行此练习是为了保持程序的速度和性能。

这些其他线程在哪里外包? (Where Are These Other Threads Outsourced?)

When using Nodejs, libuv a special library module to carry out asynchronous operations. It also used together with the back-logic of Node in order to manage a special thread pool called the libuv thread pool.

使用Nodejs时,libuv是一个特殊的库模块来执行异步操作。 它还与Node的后台逻辑一起使用,以管理称为libuv线程池的特殊线程池。

Though the thread pool is of four threads which are to delegate operations that are too heavy for the event loop. The above-mentioned long-running tasks in the event loop logic represent are too expensive for the event loop.

尽管线程池由四个线程组成,这些线程将委派对于事件循环而言过于繁重的操作。 上述事件循环逻辑中长时间运行的任务对于事件循环而言过于昂贵。

事件循环是像堆栈一样的结构吗? (Is An Event Loop Is A Stack-Like Structure?)

For instance, whenever a stack-like structure involves in any tedious process. It expects a more precise output would be an event loop that consists of a series of phases. Each phase works with its own specific tasks, and processes in a circular repetitive way.

例如,只要类似堆栈的结构参与任何繁琐的过程。 它期望更精确的输出将是一个由一系列阶段组成的事件循环。 每个阶段都有自己的特定任务,并以循环重复的方式进行处理。

结论 (CONCLUSION)

If you still have any Nodejs development queries, simply contact us to get informed.

如果您仍然对Nodejs开发有任何疑问,只需联系我们以获取通知。

翻译自: https://medium.com/@Haribabu_DM/the-perfect-architecture-of-nodejs-how-it-works-technically-3af9d3cb04f

nodejs架构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值