简体   繁体   English

node.js事件循环何时终止?

[英]When does node.js event loop terminate?

I am wondering what are the conditions under which node.js terminates the event loop. 我想知道在什么条件下node.js终止事件循环。 How does node.js figure out that no further events are going to be triggered ? node.js如何确定没有其他事件要触发? For eg in the case of an http client or a file reading application. 例如,对于http客户端或文件读取应用程序。

i'd recommend you watch this great video Philip Roberts: What the heck is the event loop anyway? 我建议您观看此精彩视频Philip Roberts:无论如何,事件循环到底是什么? | | JSConf EU 2014 JSConf欧盟2014

To explain briefly, Javascript runtime can do one thing at a time, thus it is called "single threaded". 简单地说,Javascript运行时一次只能做一件事,因此被称为“单线程”。 However, you have the reason that we can do things concurrently like HTTP requests .. etc. is due what he calls on the video as "Browser APIs" for the browser and for a backend environment like Node.js it is the C++ APIs. 但是,您有一个原因,我们可以像HTTP请求一样并发执行操作,等等。这是由于他在视频中将其称为“浏览器API”,用于浏览器,而对于诸如Node.js的后端环境,则是C ++ API。 Now, any of these "external" API calls like the HTTP client call, is pushed to a task queue. 现在,所有这些“外部” API调用(如HTTP客户端调用)都被推送到任务队列。 The event loop has one very simple job. 事件循环有一项非常简单的工作。 It will always take one job from the task queue given that the main callstack is empty and push that to the execution/running stack. 假设主调用栈为空,它将始终从任务队列中执行一项作业,并将其推送到执行/运行栈。 So the event loop will terminate when there are no jobs in the task queue (external API calls) 因此,当任务队列中没有作业(外部API调用)时,事件循环将终止

I recommend watching the full video for better understanding :) 我建议观看完整的视频,以便更好地了解:)

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

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