Node.js可以排队多少个事件? [英] How many events can Node.js queue?

查看:117
本文介绍了Node.js可以排队多少个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我看到的情况来看,如果Node中的事件需要很长时间"才能调度,Node将创建某种事件队列",并尽快将其逐个触发.

From what I see, if an event in Node take a "long time" to be dispatched, Node creates some kind of "queue of events", and they are triggered as soon as possible, one by one.

此队列多长时间?

推荐答案

虽然这似乎是一个简单的问题,但实际上是一个相当复杂的问题.不幸的是,没有任何人可以给你简单的数字.

While this may seem like a simple question, it is actually a rather complex problem; unfortunately, there's no simple number that anyone can give you.

首先:墙上的时间实际上并没有在这里起到任何作用.无论事件是否花费很长时间",所有事件都以相同的方式调度.换句话说,所有事件都通过队列"传递.

First: wall time doesn't really play a part in anything here. All events are dispatched in the same fashion, whether or not things are taking "a long time." In other words, all events pass through a "queue."

第二:没有单个队列.在很多地方,可以将各种事件分派到JS中. (以下假设您知道什么是滴答声.)

Second: there is no single queue. There are many places where different kinds of events can be dispatched into JS. (The following assumes you know what a tick is.)

  • 有些东西(或您使用的库)传递给process.nextTick().在当前报价的末尾调用它们,直到nextTick队列为空.
  • 您(或您使用的库)有一些东西传递给setImmediate().在下一个刻度线开始时将调用它们. (这意味着nextTick任务可以将事物无限期地添加到当前滴答中,从而防止了其他操作的发生,而setImmediate任务只能将事物添加到队列中以用于下一个滴答.)
  • I/O事件由 libuv 通过Linux上的epoll/kqueue/IOCP处理/Mac/Windows.当操作系统通知libuv I/O发生时,它依次调用JS中的相应处理程序.事件循环的给定滴答可能处理零个或多个I/O事件.如果刻度线需要很长时间,则I/O事件将在操作系统队列中排队.
  • 操作系统发送的
  • 信号.
  • 在单独的线程上执行的本机代码(C/C ++)可以调用JS函数.通常,这是通过 libuv工作队列完成的.
  • There are the things you (or the libraries you use) pass to process.nextTick(). They are called at the end of the current tick until the nextTick queue is empty.
  • There are the things you (or the libraries you use) pass to setImmediate(). They are called at the start of the next tick. (This means that nextTick tasks can add things to the current tick indefinitely, preventing other operations from happening whereas setImmediate tasks can only add things to the queue for the next tick.)
  • I/O events are handled by libuv via epoll/kqueue/IOCP on Linux/Mac/Windows respectively. When the OS notifies libuv that I/O has happened, it in turn invokes the appropriate handler in JS. A given tick of the event loop may process zero or more I/O events; if a tick takes a long time, I/O events will queue in an operating system queue.
  • Signals sent by the OS.
  • Native code (C/C++) executed on a separate thread may invoke JS functions. This is usually accomplished through the libuv work queue.

由于可能有很多地方需要排队,所以很难回答当前正在排队的商品有多少" ,远远小于这些队列的绝对限制.本质上,任务队列大小的硬限制是可用RAM.

Since there are many places where work may be queued, it is not easy to answer "how many items are currently queued", much less what the absolute limit of those queues are. Essentially, the hard limit for the size of your task queues is available RAM.

实际上,您的应用将:

  • 命中V8堆约束
  • 对于I/O,请最大使用允许的打开文件描述符的数量.

...早在任何队列的大小出现问题之前.

...well before the size of any queue becomes problematic.

如果您只是对您的应用是否承受重负荷感兴趣,则 toobusy 可能令人感兴趣-它将对事件循环的每个滴答声进行计时,以确定您的应用程序是否在处理每个滴答声上花费了不寻常的时间(这可能表明您的任务队列非常大).

If you're just interested in whether or not your app under heavy load, toobusy may be of interest -- it times each tick of the event loop to determine whether or not your app is spending an unusual amount of time processing each tick (which may indicate that your task queues are very large).

这篇关于Node.js可以排队多少个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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