当前位置:网站首页>Event_ Loop event loop mechanism
Event_ Loop event loop mechanism
2022-07-28 07:05:00 【SheepOnTheCloud】
One , What is? Event_loop
Event LoopIt's an event cycle , Refers to the browser orNodeA solution tojavaScriptA mechanism by which the single thread runtime does not block , That is, we often use asynchronous Principle .
Two , Process model of event loop
1, Select the current task queue to execute , Select the first task in the task queue , If the task queue is empty , namely null, Then execute jump to micro task (MicroTaks) The implementation steps of
2, Set the task in the event loop to the selected task
3, Perform tasks
4, Set the currently running task in the event loop to null
5, Delete the completed task from the task queue
6,microtasks step , Get into microtask checkpoint
7, Update interface rendering
8, Step one back

Execute entry microtask At the checkpoint , The user agent performs the following steps :
Set up microtask The checkpoint is marked as true.
When events loop microtask When the execution is not empty : Choose the one that enters first microtask Queued microtask, Will be a circular event microtask Set to selected microtask, function microtask, Will execute the completed microtask by null, Removed from the microtask Medium microtask.
clear IndexDB Business
Set to enter microtask The checkpoint is marked as false.
Link to the original text :https://blog.csdn.net/weixin_53504991/article/details/115455041

3、 ... and ,NodeJS Of Event Loop
Node Medium Event Loop Is based on libuv Realized , and libuv yes Node The new cross platform abstraction layer ,libuv Use asynchronous , Event driven programming , The core is to provide i/o Event loops and asynchronous callbacks .libuv Of API Including time , Non blocking network , Asynchronous file operations , Subprocesses and so on . Event Loop Is in the libuv Implemented in .
What is? libuv?
Libuv yes Node.js Multi platform support library , Mainly used for asynchronous I/O. It is mainly for Node.js Developed , as time goes on , It is widely used in other systems , Such as Luvit、pyuv、Julia etc. .Libuv Basically for platform dependent libev/IOCP The abstraction of , Provide users with information based on libev Of API.
libuv Some important features of are :
- Support full function event loop
- File system events
- Asynchronous file and file system operations
- asynchronous TCP and UDP Socket
- Subprocesses

Node Of Event loop Divided into 6 Stages , The details are as follows :

- timers: perform setTimeout and setInterval Medium maturity callback.
- pending callback: A few in the last cycle callback It will be implemented at this stage .
- idle, prepare: For internal use only .
- poll: The most important stage , perform pending callback, Where appropriate, back blocking at this stage .
- check: perform setImmediate(setImmediate() Is to insert an event at the end of the event queue , The functions of the main thread and the event queue are executed immediately after execution setImmediate The specified callback function ) Of callback.
- close callbacks: perform close The event callback, for example socket.on('close'[,fn]) perhaps http.server.on('close, fn).
- Node browser-specific Event Loop differences
- In the browser environment ,microtask The task queue is each macrotask Execute after execution . And in the Node.js in ,microtask It is executed between the stages of the event loop , That is, a phase is completed , They will execute microtask Queue tasks .
summary
The browser and Node In the environment ,microtask Task queues are executed at different timesNode End ,microtask Execute between phases of the event loop
Browser side ,microtask In the event loop macrotask Execute after execution
边栏推荐
- Repair the faulty sector
- Shell script - regular expression
- 防火墙——iptables防火墙(四表五链、防火墙配置方法、匹配规则详解)
- LNMP搭建过程详解
- YUM仓库的搭建
- MOOC翁恺C语言第七周:数组运算:1.数组运算2.搜索3.排序初步
- MySQL build database Series (I) -- download MySQL
- ---Stack & queue---
- Vmware workstation configuration net mode
- Esxi community network card driver updated again
猜你喜欢

Applet creation component

MOOC翁恺C语言第八周:指针与字符串:1.指针2.字符类型3.字符串4.字符串计算

Svg understanding and drawing application

On cookies and session

MySQL build database Series (I) -- download MySQL

MOOC Weng Kai C language fourth week: further judgment and circulation: 1. Logical types and operations 2. Judgment of cascading and nesting

起点中文网 字体反爬技术 网页可以显示数字字母 网页代码是乱码或空格

Detailed explanation of LNMP construction process

Esxi community network card driver

修复故障扇区
随机推荐
Escape character notes
Traversal binary tree
Results fill in the blanks carelessly (violent solution)
Three cache technologies -- localstorage, sessionstorage, cookies
[learning notes] coding ability
Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)
Applet navigator cannot jump (debug)
Firewall - iptables firewall (four tables and five links, firewall configuration method, detailed explanation of matching rules)
Create, traverse and search nodes as required for single linked list
About gcc:multiple definition of
Applets: lifecycle
DOM - Events
Clock tree analysis example
远程访问云服务器上Neo4j等服务的本地网址
My deployment notes
SSH service configuration
JS array method Encyclopedia
Detailed explanation of LNMP construction process
[learning notes] thread creation
[learning notes] process creation