Nodejs事件循环 [英] Nodejs Event Loop

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

问题描述

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




  • libev / libuv

  • v8 javascript事件循环



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



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



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

解决方案

我个人阅读node.js&的源代码v8。



当我尝试理解node.js架构以编写本机模块时,我遇到了类似的问题。



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


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


  2. LibEio 是一个库,用于异步执行输入输出。它处理文件描述符,数据处理程序,套接字等。您可以在这里阅读更多相关信息这里


  3. LibUv 是libeio,libev,c-ares(用于DNS)和iocp(用于windows asynchronous-io)的顶层的抽象层。 LibUv执行,维护和管理事件池中的所有io和事件。 (在libeio线程池的情况下)。你应该在libUv上查看 Ryan Dahl的教程。这将开始让你更了解libUv如何工作,然后你将了解node.js如何在libuv和v8的顶部工作。


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





了解如何将libeio与node.js一起使用以创建一个你应该看到的同步模块这个例子



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



例如:

  eio_custom(任务,FLAG ,AfterTask,Eio_REQUEST); 

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


Are there internally two event loops in nodejs architecture?

  • libev/libuv
  • v8 javascript event loop

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?

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

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

解决方案

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

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

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

  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.

  2. 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.

  3. 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.

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

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

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 ].

For example:

eio_custom(Task,FLAG,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天全站免登陆