node.js 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9497076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:15:24  来源:igfitidea点击:

How node.js works?

node.jsparallel-processing

提问by Davita

I don't understand several things about nodejs. Every information source says that node.js is more scalable than standard threaded web servers due to the lack of threads locking and context switching, but I wonder, if node.js doesn't use threads how does it handle concurrent requests in parallel? What does event I/O model means?

我不明白关于 nodejs 的几件事。每个信息源都说 node.js 由于缺少线程锁定和上下文切换而比标准线程 Web 服务器更具可扩展性,但我想知道,如果 node.js 不使用线程,它如何并行处理并发请求?事件 I/O 模型是什么意思?

Your help is much appreciated. Thanks

非常感谢您的帮助。谢谢

回答by Michael Borgwardt

Node is completely event-driven. Basically the server consists of one thread processing one event after another.

Node 是完全事件驱动的。基本上,服务器由一个线程组成,处理一个又一个事件。

A new request coming in is one kind of event. The server starts processing it and when there is a blocking IO operation, it does not wait until it completes and instead registers a callback function. The server then immediately starts to process another event (maybe another request). When the IO operation is finished, that is another kind of event, and the server will process it (i.e. continue working on the request) by executing the callback as soon as it has time.

传入的新请求是一种事件。服务器开始处理它,当有阻塞 IO 操作时,它不会等到它完成,而是注册一个回调函数。然后服务器立即开始处理另一个事件(可能是另一个请求)。当 IO 操作完成时,这是另一种事件,服务器将在有时间时通过执行回调来处理它(即继续处理请求)。

So the server never needs to create additional threads or switch between threads, which means it has very little overhead. If you want to make full use of multiple hardware cores, you just start multiple instances of node.js

所以服务器永远不需要创建额外的线程或在线程之间切换,这意味着它的开销很小。如果你想充分利用多个硬件核心,你只需启动多个 node.js 实例

UpdateAt the lowest level (C++ code, not Javascript), there actually are multiple threadsin node.js: there is a pool of IO workers whose job it is to receive the IO interrupts and put the corresponding events into the queue to be processed by the main thread. This prevents the main thread from being interrupted.

更新在最底层(C++ 代码,而不是 Javascript),node.js 中实际上有多个线程:有一个 IO 工作池,其工作是接收 IO 中断并将相应的事件放入队列中进行处理通过主线程。这可以防止主线程被中断。

回答by Alok Deshwal

Although Question is already explained before a long time, I'm putting my thoughts on the same.

虽然问题已经解释了很长时间,但我还是把我的想法放在同一个地方。

Node JS is single threaded technology. Basically Node JS creator( Ryan Dahl ) concern was that parallel processing using multiple threads is not the right way or too complicated.

Node JS 是单线程技术。基本上,Node JS 创建者 (Ryan Dahl) 担心的是,使用多线程进行并行处理不是正确的方式或过于复杂。

if node.js doesn't use threads how does it handle concurrent requests in parallel

如果 node.js 不使用线程,它如何并行处理并发请求

Ans:It's completely wrong sentence when you say it doesn't use threads, Node Js use threads but in a smart way. It uses single thread to serve all the HTTP requests & multiple threads in thread pool(in libuv) for handling any blocking operation

Ans:你说它不使用线程是完全错误的句子,Node Js 使用线程但是以一种聪明的方式。它使用单线程来处理所有的 HTTP 请求和线程池中的多个线程(在 libuv 中)来处理任何阻塞操作

Libuv:A library to handle asynchronous I/O.

Libuv:处理异步 I/O 的库。

What does event I/O model means?

事件 I/O 模型是什么意思?

Ans: The right term is non-blocking I/O. It almost never blocks as Node JS official site says. When any request goes to node server it never queues the request. It take request and start executing if it's blocking operation then it's been sent to working threads area and registered a callback for the same as soon as code execution get finished, it trigger the same callback and goes to event queue and processed by event loop again after that create response and send to the respective client.

答:正确的术语是非阻塞 I/O。它几乎不会像 Node JS 官方网站所说的那样阻塞。当任何请求到达节点服务器时,它永远不会将请求排队。它接受请求并开始执行,如果它是阻塞操作然后它被发送到工作线程区域并在代码执行完成后立即注册一个回调,它触发相同的回调并进入事件队列并再次由事件循环处理创建响应并发送到相应的客户端。

Useful link: click here

有用的链接: 点击这里

回答by Mazumder Nazmul

Node JS is a JavaScript runtime environment. Both browser and Node JS run on V8 JavaScript engine. Node JS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node JS applications uses single threaded event loop architecture to handle concurrent clients. Actually its' main event loop is single threaded but most of the I/O works on separate threads, because the I/O APIs in Node JS are asynchronous/non-blocking by design, in order to accommodate the main event loop. Consider a scenario where we request a backend database for the details of user1 and user2 and then print them on the screen/console. The response to this request takes time, but both of the user data requests can be carried out independently and at the same time. When 100 people connect at once, rather than having different threads, Node will loop over those connections and fire off any events your code should know about. If a connection is new it will tell you .If a connection has sent you data, it will tell you .If the connection isn't doing anything ,it will skip over it rather than taking up precision CPU time on it. Everything in Node is based on responding to these events. So we can see the result, the CPU stay focused on that one process and doesn't have a bunch of threads for attention.There is no buffering in Node.JS application it simply output the data in chunks.

Node JS 是一个 JavaScript 运行时环境。浏览器和 Node JS 都运行在 V8 JavaScript 引擎上。Node JS 使用事件驱动的非阻塞 I/O 模型,使其轻量级和高效。Node JS 应用程序使用单线程事件循环架构来处理并发客户端。实际上,它的主事件循环是单线程的,但大多数 I/O 工作在单独的线程上,因为 Node JS 中的 I/O API 在设计上是异步/非阻塞的,以适应主事件循环。考虑一个场景,我们请求后端数据库获取 user1 和 user2 的详细信息,然后将它们打印在屏幕/控制台上。对这个请求的响应需要时间,但是这两个用户数据请求可以独立地同时进行。当 100 人同时连接时,而不是使用不同的线程,Node 将遍历这些连接并触发您的代码应该知道的任何事件。如果一个连接是新的,它会告诉你。如果一个连接向你发送了数据,它会告诉你。如果连接没有做任何事情,它会跳过它而不是占用精确的 CPU 时间。Node 中的一切都基于对这些事件的响应。所以我们可以看到结果,CPU 一直专注于那个进程,没有一堆线程来关注。Node.JS 应用程序中没有缓冲,它只是以块的形式输出数据。Node 中的一切都基于对这些事件的响应。所以我们可以看到结果,CPU 一直专注于那个进程,没有一堆线程来关注。Node.JS 应用程序中没有缓冲,它只是以块的形式输出数据。Node 中的一切都基于对这些事件的响应。所以我们可以看到结果,CPU 一直专注于那个进程,没有一堆线程来关注。Node.JS 应用程序中没有缓冲,它只是以块的形式输出数据。