[英]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.注意:提到的回调队列是事件队列,调用堆栈是执行处理程序。
Credit to packtpub.com归功于 packtpub.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.