node.js事件队列在哪里? [英] Where is the node.js event queue?

查看:80
本文介绍了node.js事件队列在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在堆栈溢出中看到了类似的问题,但是没有一个问题完全陷入我所遇到的问题吗?我熟悉事件队列,它们如何工作以及如何实现。我是Node.js的新手,我想把头放在Node.js的工作方式上。

I have seen similar questions on stack overflow but none of them fully dive down into the question that I have? I am familiar with event queues, how they work as well as implementing them. I am new to node.js and I am trying to wrap my head around how Node.js does it.

在c ++应用程序中,您可能会做一些事情:

In a c++ application you would do something along the lines of:

int main(){
    std::vector<Handler*> handlers;
    BlockingQueue queue = new BlockingQueue();
    //Add all the handlers call constructors and other such initialization

    //Then run the event loop
    while(true){
        Event e = queue.pop();

        for( std::vector<Handler>::iterator it = handlers.begin(); it != handlers.end(); ++it){
            *it.handle(e);
         }
     }
}

现在对于节点.js我可能有一个看起来像main.js的主文件。

Now in the case of node.js I might have a main file called main.js that looks like.

var http = require("http");
function main(){
    // Console will print the message
    console.log('Server running at http://127.0.0.1:8080/');
    var server = http.createServer(function (request, response) {

        // Send the HTTP header
        // HTTP Status: 200 : OK
        // Content Type: text/plain
        response.writeHead(200, {'Content-Type': 'text/plain'});

        // Send the response body as "Hello World"
        response.end('Hello World\n');
    });

    server.listen(8080);
    console.log('Main completed');
}

main();

我了解server.listen正在将处理程序附加到事件队列,并且我们要添加回调与c ++示例类似。

I understand the server.listen is attaching a handler to the event queue and that we are adding the callback similar to the c++ example.

我的问题是。事件队列在哪里?它在javascript的某个地方还是内置在解释器中?

My question is. Where is the event queue? Is it in the javascript somewhere or is it built into the interpreter? Also how does the main function get called relative to the main event loop?

推荐答案


在哪里?事件队列?是在javascript中还是在解释器中内置了

Where is the event queue? Is it in the javascript somewhere or is it built into the interpreter?

事件队列已内置在操作环境中托管Java语言解释器。它不是Java脚本本身的基础,因此它不是实际JS运行时的一部分。一个有趣的指示是 setTimeout()实际上不是ECMAScript的一部分,而是主机可以在Javascript环境中使用的东西。

The event queue is built into the operating environment that hosts the Javascript interpreter. It isn't fundamental to Javascript itself so it's not part of the actual JS runtime. One interesting indicator of this is that setTimeout() is not actually part of ECMAScript, but rather something made available to the Javascript environment by the host.

node.js中围绕Java脚本实现的系统会跟踪外部触发的事件(计时器,联网结果等),并且当Java脚本不忙于执行某些事情并且发生外部事件时,它将然后触发关联的Javascript回调。如果Javascript忙于执行某事,则它将该事件放入队列,以便Javascript不再繁忙时,它便可以触发队列中的下一个事件。

The system surrounding the Javascript implementation in node.js keeps track of externally triggered events (timers, networking results, etc...) and when Javascript is not busy executing something and an external event occurs, it then triggers an associated Javascript callback. If Javascript is busy executing something, then it queues that event so that as soon as Javascript is no longer busy, it can then trigger the next event in the queue.

node .js本身将 libuv 用于事件循环。您可以在此处了解更多信息。它提供了一种针对node.js开发的事件化,异步I / O的多平台方式,但也被其他一些项目使用。

node.js itself uses libuv for the event loop. You can read more about that here. It provides a multi-platform way of doing evented, async I/O that was developed for node.js, but is also being used by some other projects.

相关的答案可能也有帮助:

Here's a related answer that might also help:

在节点中等待回调时运行任意代码?


相对于主事件
循环,是否调用了main函数?

Also how does the main function get called relative to the main event loop?

node.js启动时,给出了要执行的初始脚本文件。它将脚本文件加载到内存中,解析其中的Javascript并执行它。在您的特定示例中,这将导致函数 main 被解析,然后导致执行 main()

When node.js starts up, it is given an initial script file to execute. It loads that script file into memory, parses the Javascript in it and executes it. In your particular example, that will cause the function main to get parsed and then will cause the execution of main() which will run that function.

加载,解析和执行启动时传递给node的脚本文件是给node.js的任务。它根本与事件队列无关。在某些node.js应用程序中,它运行该初始脚本,然后退出(完成其工作)。在其他node.js应用程序中,初始脚本启动计时器或服务器或类似的东西,这些东西将来会接收事件。在这种情况下,node.js会运行初始脚本以完成操作,但是由于现在创建了持久对象并且正在侦听事件(在您的情况下为服务器),因此nodejs不会关闭应用程序。它使它保持运行状态,以便将来发生这些事件时可以接收这些事件。

Loading, parsing and executing the script file passed to node when it starts up is the task given to node.js. It isn't really related to the event queue at all. In some node.js applications, it runs that initial script and then exits (done with its work). In other node.js applications, the initial script starts timers or servers or something like that which will receive events in the future. When that is the case, node.js runs the initial script to completion, but because there are now lasting objects that were created and are listening for events (in your case, a server), nodejs does not shut down the app. It leaves it running so that it can receive these future events when they occur.

这里缺少的一件东西是创建的服务器对象允许您注册一个回调,该回调在将来发生某些特定事件时将被调用一次或多次。此行为未内置在Javascript中。相反,实现这些对象或它们使用的TCP函数的代码必须维护已注册的回调的列表,并且当这些事件发生时,它必须执行代码,以便调用适当的回调并传递适当的数据。在 http.createServer()的情况下,它是在nodejs http库中混合使用Javascript和本机代码来实现的。

One missing piece here is that things like the server object you created allow you to register a callback that will be called one or more times in the future when some particular events occur. This behavior is not built into Javascript. Instead, the code that implements these objects or the TCP functions that they use must maintain a list of callbacks that are registered and when those events occur, it must execute code so that the appropriate callbacks are called and passed the appropriate data. In the case of http.createServer(), it is a mix of Javascript and native code in the nodejs http library that make that work.

这篇关于node.js事件队列在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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