Event polling and execution of asynchronous code in node
世界只因有你
世界只因有你 2017-07-05 10:39:22
0
2
737

The main thread of node executes js code. When it is an asynchronous operation, node will hand it over to libuv for execution, and then execute the code behind the asynchronous operation. After libuv is executed, it will be placed in a circular queue. libuv will have an event loop (Event Loop) similar to while(true). This event loop exists in the single thread of the node (that is, the main thread). The main thread loops Get the event execution callback function.
So what is the process of the main thread looping to retrieve events and executing the asynchronous code behind it? It is impossible for a thread to do two things at the same time. After executing the asynchronous code, will the CPU loop to fetch events when it is idle?

世界只因有你
世界只因有你

reply all(2)
过去多啦不再A梦

首先明确一点,主线程在有代码执行时是不会去轮询的。
所以主线程会把所有的初始代码(非回调代码)执行完后再去事件队列轮询(事件队列在被轮询时可以看成一个js对象)因为理论上js引擎主线程,只能识别js代码。
但初始代码执行完后,js主线程会去轮询取事件,当取到一个回调函数后,js引擎会把当前回调代码执行完后,才会
在去轮询。
总结一下就是,只有js引擎在没有代码执行时,才会去进行轮询,或者说,js主线程中永远只有一个回调函数

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!