Nodejs 事件循环 [英] Nodejs Event Loop

查看:27
本文介绍了Nodejs 事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

nodejs 架构内部是否有两个事件循环?

Are there internally two event loops in nodejs architecture?

  • libev/libuv
  • v8 javascript 事件循环

在 I/O 请求中,节点是否将请求排入 libeio 队列,libeio 反过来使用 libev 通过事件通知数据的可用性,最后这些事件由 v8 事件循环使用回调处理?

On an I/O request does node queue the request to libeio which in turn notifies the availability of data via events using libev and finally those events are handled by v8 event loop using callbacks?

基本上,libev 和 libeio 是如何集成到 nodejs 架构中的?

Basically, How are libev and libeio integrated in nodejs architecture?

是否有任何文档可以清楚地了解 nodejs 的内部架构?

Are there any documentation available to give a clear picture of nodejs internal architecture?

推荐答案

我一直在亲自阅读node.js的源码&v8.

I have been personally reading the source code of node.js & v8.

当我试图理解 node.js 架构以编写原生模块时,我遇到了和你类似的问题.

I went into a similar problem like you when I tried to understand node.js architecture in order to write native modules.

我在这里发布的是我对 node.js 的理解,这也可能有点偏离轨道.

What I am posting here is my understanding of node.js and this might be a bit off track as well.

  1. Libev 是实际在 node.js 内部运行的事件循环执行简单的事件循环操作.它最初是为 *nix 系统编写的.Libev 为进程运行提供了一个简单但经过优化的事件循环.您可以在此处阅读有关 libev 的更多信息.

  1. Libev is the event loop which actually runs internally in node.js to perform simple event loop operations. It's written originally for *nix systems. Libev provides a simple yet optimized event loop for the process to run on. You can read more about libev here.

LibEio 是一个异步执行输入输出的库.它处理文件描述符、数据处理程序、套接字等.您可以在此处阅读有关它的更多信息 此处.

LibEio is a library to perform input output asynchronously. It handles file descriptors, data handlers, sockets etc. You can read more about it here here.

LibUv 是 libeio , libev, c-ares (对于 DNS ) 和 iocp (对于 windows asynchronous-io).LibUv 执行、维护和管理事件池中的所有 io 和事件.(在 libeio 线程池的情况下).您应该查看关于 libUv 的Ryan Dahl 的教程.这将使您对 libUv 本身的工作方式更有意义,然后您将了解 node.js 如何在 libuv 和 v8 之上工作.

LibUv is an abstraction layer on the top of libeio , libev, c-ares ( for DNS ) and iocp (for windows asynchronous-io). LibUv performs, maintains and manages all the io and events in the event pool. ( in case of libeio threadpool ). You should check out Ryan Dahl's tutorial on libUv. That will start making more sense to you about how libUv works itself and then you will understand how node.js works on the top of libuv and v8.

要仅了解 javascript 事件循环,您应该考虑观看这些视频

To understand just the javascript event loop you should consider watching these videos

要了解如何将 libeio 与 node.js 结合使用以创建异步模块,您应该查看此示例.

To see how libeio is used with node.js in order to create async modules you should see this example.

基本上在 node.js 中发生的事情是 v8 循环运行并处理所有 javascript 部分以及 C++ 模块 [当它们在主线程中运行时(根据官方文档 node.js 本身是单线程的)].当在主线程之外时,libev 和 libeio 在线程池中处理它,libev 提供与主循环的交互.所以根据我的理解,node.js 有 1 个永久事件循环:那就是 v8 事件循环.为了处理 C++ 异步任务,它使用了一个线程池 [via libeio &].

Basically what happens inside the node.js is that v8 loop runs and handles all javascript parts as well as C++ modules [ when they are running in a main thread ( as per official documentation node.js itself is single threaded) ]. When outside of the main thread, libev and libeio handle it in the thread pool and libev provide the interaction with the main loop. So from my understanding, node.js has 1 permanent event loop: that's the v8 event loop. To handle C++ async tasks it's using a threadpool [via libeio & libev ].

例如:

eio_custom(Task,FLAG,AfterTask,Eio_REQUEST);

出现在所有模块中的通常是调用线程池中的函数Task.完成后,它会调用主线程中的 AfterTask 函数.而 Eio_REQUEST 是请求处理程序,它可以是一个结构/对象,其动机是提供线程池和主线程之间的通信.

Which appears in all modules is usually calling the function Task in the threadpool. When it's complete, it calls the AfterTask function in the main thread. Whereas Eio_REQUEST is the request handler which can be a structure / object whose motive is to provide communication between the threadpool and main thread.

这篇关于Nodejs 事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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