简体   繁体   English

反应器模式在 Node.js 中是如何工作的?

[英]how does reactor pattern work in Node.js?

在此处输入图像描述

I am reading Node.js Design Patterns.我正在阅读 Node.js 设计模式。 I am stuck in the understanding of the reactor pattern.我被困在对反应器模式的理解中。 I do not see any call stack here.我在这里看不到任何调用堆栈。 I thought the call stack was one of the main parts of Node.js design.我认为调用堆栈是 Node.js 设计的主要部分之一。 Can anyone please help me understand this diagram?谁能帮我理解这张图? Also, there is no callback queue.此外,没有回调队列。

Everything starts with the application, application makes requests and the event demultiplexer gathers those requests then forms queues, Event Queues.一切都从应用程序开始,应用程序发出请求,事件多路分解器收集这些请求,然后形成队列,即事件队列。 Event demultiplexer is run by libuv which is an asynchronous IO library that allows Node.js performs I/O.事件解复用器由 libuv 运行,它是一个异步 IO 库,允许 Node.js 执行 I/O。

In the diagram you see one event queue.在图中,您会看到一个事件队列。 actually there is not only 1 event queue, there are 7 basics queues.实际上不仅有 1 个事件队列,还有 7 个基本队列。 those queues have ascending priorities, the queue that highest priority checked first by the event loop.这些队列具有升序优先级,事件循环首先检查最高优先级的队列。

Timers queue has the highest priority.定时器队列具有最高优先级。 setTimeout and setInterval functions get queued here. setTimeout 和 setInterval 函数在这里排队。 Once the events are done in this queue, or time is up, event loop passes those functions to call stack, in the diagram it is named execute handler.一旦事件在这个队列中完成,或者时间到了,事件循环将这些函数传递给调用堆栈,在图中它被命名为执行处理程序。

Once one of the event queues are done, instead of jumping to next queue, event loop firstly will check 2 other queues which queues other micro tasks and process.nextTick functions.一旦其中一个事件队列完成,而不是跳转到下一个队列,事件循环首先将检查其他 2 个队列,这些队列将其他微任务和 process.nextTick 函数排队。 Then it will jump to next queue.然后它会跳到下一个队列。 this diagram will help u visualize the event loop.此图将帮助您可视化事件循环。 在此处输入图像描述

If there are no events in the event queue or the Event Demultiplexer has no pending requests, the program will complete.如果事件队列中没有事件或事件解复用器没有挂起的请求,则程序将完成。

note:callback queue that mentioned is event queue and call stack is execute handler.注意:提到的回调队列是事件队列,调用堆栈是执行处理程序。

  1. The application generates a new I/O operation by submitting a request to the Event Demultiplexer.The application also specifies a handler, which will be invoked when the operation completes.应用程序通过向 Event Demultiplexer 提交请求来生成新的 I/O 操作。应用程序还指定一个处理程序,该处理程序将在操作完成时调用。 Submitting a new request to the Event Demultiplexer is a non-blocking call and it immediately returns control to the application.向 Event Demultiplexer 提交新请求是一个非阻塞调用,它会立即将控制权返回给应用程序。
  2. When a set of I/O operations completes, the Event Demultiplexer pushes a set of corresponding events into the Event Queue.当一组 I/O 操作完成时,Event Demultiplexer 将一组相应的事件推送到 Event Queue 中。
  3. At this point, the Event Loop iterates over the items of the Event Queue.此时,事件循环遍历事件队列的项目。
  4. For each event, the associated handler is invoked.对于每个事件,都会调用关联的处理程序。
  5. The handler, which is part of the application code, gives back control to the Event Loop when its execution completes (5a).处理程序是应用程序代码的一部分,在事件循环执行完成时将控制权交还给事件循环 (5a)。 While the handler executes, it can request new asynchronous operations (5b), causing new items to be added to the Event Demultiplexer (1).在处理程序执行时,它可以请求新的异步操作 (5b),从而将新项目添加到事件多路分解器 (1)。
  6. When all the items in the Event Queue are processed, the Event Loop blocks again on the Event Demultiplexer, which then triggers another cycle when a new event is available.当事件队列中的所有项目都处理完毕后,事件循环再次阻塞事件解复用器,然后当有新事件可用时触发另一个循环。

Credit to packtpub.com归功于 packtpub.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM