te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - how does reactor pattern work in Node.js? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how does reactor pattern work in Node.js? - Stack Overflow

programmeradmin3浏览0评论

I am reading Node.js Design Patterns. 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. Can anyone please help me understand this diagram? Also, there is no callback queue.

I am reading Node.js Design Patterns. 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. Can anyone please help me understand this diagram? Also, there is no callback queue.

Share Improve this question edited Jan 8, 2022 at 7:17 Yilmaz asked Jun 16, 2019 at 20:23 YilmazYilmaz 49.5k18 gold badges215 silver badges268 bronze badges 1
  • Yeah, I think a diagram like that would confuse anyone... if you were to just look at some example code rather than trying to interpret plicated diagrams that don't really clarify anything, I think whatever the "reactor pattern" is would be a lot clearer to you. My guess is, based on the name, that it means "event-based programming" which is very natural to write in Node.js. – Patrick Roberts Commented Jun 30, 2019 at 4:06
Add a ment  | 

2 Answers 2

Reset to default 9

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.

In the diagram you see one event queue. actually there is not only 1 event queue, there are 7 basics queues. 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. 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. 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 plete.

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 pletes. Submitting a new request to the Event Demultiplexer is a non-blocking call and it immediately returns control to the application.
  2. When a set of I/O operations pletes, the Event Demultiplexer pushes a set of corresponding events into the 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 pletes (5a). While the handler executes, it can request new asynchronous operations (5b), causing new items to be added to the Event Demultiplexer (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.

发布评论

评论列表(0)

  1. 暂无评论