简体   繁体   English

是否所有节点js请求最初都发送到回调队列? Node.js如何管理并发用户?

[英]Are all node js requests initially sent to callback queue? How does Node.js manage concurrent users?

What will happen if there is no IO in current scenario? 如果当前方案中没有IO,将会发生什么? Will the requests get executed in synchronous way and each request will have to wait for previous request? 这些请求是否将以同步方式执行,并且每个请求都必须等待上一个请求?

Are all node.js request initially sent to callback queue? 是否所有的node.js请求最初都发送到回调队列? Otherwise wont the stack throw stack overflow in case all requests are being served by stack... 否则,如果所有请求都由堆栈服务,则堆栈不会引发堆栈溢出...

I have just started using node and completely not sure about how things are working? 我刚刚开始使用节点,完全不确定事情如何进行? Every online site says think of event loop as a waiter in a restaurant taking order but I do not get how node handles requests in case there are hundreds of request burst load. 每个在线站点都将事件循环视为餐厅中的服务员来下订单,但是如果有成百上千的请求突发负载,我将不了解节点如何处理请求。 Will these request be kept on hold in some sort of queue? 这些请求是否会保留在某种队列中?

The Event demultiplexer is a notification-issuing interface within the Node JS. 事件多路分解器是Node JS中的通知发出接口。 It is used to gather every request from watched sources in form of an event and queues each event in a queue. 它用于以事件的形式收集来自监视源的每个请求,并将每个事件排入队列。 It is the demultiplexer that forms the Event Queue. 是构成事件队列的多路分解器。 Event demultiplexer is an API run by Libuv. 事件多路分解器是由Libuv运行的API。 在此处输入图片说明

All the collected requests are distributed in those different event queues. 所有收集的请求都分布在这些不同的事件队列中。 The queue at the top takes highest priority, and the queue at the bottom takes the least. 顶部的队列具有最高优先级,底部的队列具有最低优先级。

So, event loop checks timers queue first and if there are events that are ready to be passed to call stack with its callback to be executed, event loop will handle it. 因此,事件循环首先检查计时器,如果有事件准备好传递给调用堆栈并执行其回调,则事件循环将对其进行处理。 After event loop is done with timers queues, however before jumping to next queue, event loop will look at 2 other queues that run by node itself, and will handle those queues first. 在使用计时器队列完成事件循环之后,但是在跳转到下一个队列之前,事件循环将查看由节点本身运行的其他2个队列,并将首先处理这些队列。 those are: 那些是:

Next Ticks Queue — Callbacks added using process.nextTick function Other Microtasks Queue — Includes other microtasks such as resolved promise callbacks, console.log etc. Next Ticks Queue —使用process.nextTick函数添加的回调Other Microtasks Queue —包括其他微任务,例如已解决的Promise回调,console.log等。

After event loop is done with those 2 queues, then it will move to next queue which is I/O callbacks. 在使用这2个队列完成事件循环之后,它将移至下一个队列,即I / O回调。

Note that Node event loop is single threaded. 请注意,Node事件循环是单线程的。 However for computationally intensive or time consuming tasks libuv has thread pool which by default has 4 pools, not to block the event loop. 但是,对于计算量大或耗时的任务,libuv具有线程池,默认情况下它具有4个池,而不是阻塞事件循环。 There are only four things that use this thread pool - DNS lookup, fs, crypto and zlib. 使用此线程池的只有四件事-DNS查找,fs,crypto和zlib。 Everything else executes in the main thread irrespective or blocking or non blocking. 不论是否阻塞,其他所有事情都在主线程中执行。

So far we had event loop which handles events in event queues and thread pool that run by libuv for some other functions. 到目前为止,我们有了事件循环,该循环处理libuv为其他某些功能运行的事件队列和线程池中的事件。 Node standard library also has some functions that make use of code that is built into the underlying operating system through libuv. 节点标准库还具有一些功能,这些功能利用了通过libuv内置到底层操作系统中的代码。 All modern operating systems provide APIs to interact with libuv. 所有现代操作系统都提供与libuv交互的API。

Libuv delegates some tasks like network I/O and fs module's functions to operating system, so OS runs those tasks and brings the results to Libuv. Libuv将一些任务(如网络I / O和fs模块的功能)委派给操作系统,因此OS运行这些任务并将结果带给Libuv。

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

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