当前位置:网站首页>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
边栏推荐
- Es6--- > arrow function, class, modularization
- Canvas drawing 1
- 1、 PXE overview and installation
- Method of designing test cases
- MOOC翁恺C语言 第四周:进一步的判断与循环:3.多路分支4.循环的例子5.判断和循环常见的错误
- Results fill in the blanks carelessly (violent solution)
- Group management and permission management
- SSH service configuration
- MOOC Weng Kai C language fourth week: further judgment and circulation: 3. Multiple branches 4. Examples of circulation 5. Common errors in judgment and circulation
- On cookies and session
猜你喜欢

Bond mode configuration

Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)

MOOC Weng Kai C language week 8: pointer and string: 1. Pointer 2. Character type 3. String 4. String calculation

一、PXE概述和安装

Wechat applet custom compilation mode

Neo4j运行报错Error occurred during initialization of VM Incompatible minimum and maximum heap sizes spec

DHCP service

Wangeditor (@4.7.15) - lightweight rich text editor

Technology sharing | do you know the functions of the server interface automated testing and requests library?

1、 PXE overview and installation
随机推荐
Results fill in the blank. How many days of national day are Sundays (solved by pure Excel)
My deployment notes
RAID磁盘阵列
Ubuntu18.04+centos7 configure redis master-slave [learning notes]
Shell script - regular expression
MySQL installation and use
Custom component -- pure data field & component life cycle
DOM operation cases
DNS domain name resolution service
Erudite Valley Learning Records] Super summary, attentive sharing | common APIs
爬虫学习总结
Pictures are adaptive to the screen
Esxi community network card driver
Esxi community network card driver updated again
Applets: lifecycle
Technology sharing | do you know the functions of the server interface automated testing and requests library?
[learning notes] knowledge management
How to describe a bug and the definition and life cycle of bug level
[learning notes] drive
Result number of filled briquettes