当前位置:网站首页>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
边栏推荐
- Small turtle C (Chapter 6 arrays 1 and 2)
- 小甲鱼C(第六章数组1、2)
- Applet creation component
- SSH service configuration
- Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)
- DHCP原理与配置
- Applet custom components - data, methods, and properties
- Differences and relationships among NPM, Yran and NPX
- Principle and configuration of NAT and pat
- Traversal binary tree
猜你喜欢

SSH服务配置

Custom component -- pure data field & component life cycle

bond模式配置

RAID disk array

主动扫描技术nmap详解

MOOC Weng Kai C language week 6: arrays and functions: 1. Arrays 2. Definition and use of functions 3. Parameters and variables of functions 4. Two dimensional arrays

Applet navigator cannot jump (debug)
![[learning records of erudite Valley] Super summary, attentive sharing | collection](/img/a3/4183a074a7cdc41fe7624624492280.png)
[learning records of erudite Valley] Super summary, attentive sharing | collection

YUM仓库的搭建

How to describe a bug and the definition and life cycle of bug level
随机推荐
Clock tree analysis example
shell脚本——编程条件语句(条件测试、if语句、case分支语句、echo用法、for循环、while循环)
How to describe a bug and the definition and life cycle of bug level
Uni app double click event simulation
Applet creation component
Wechat applet custom compilation mode
Pictures are adaptive to the screen
MOOC Weng Kai C language week 6: arrays and functions: 1. Arrays 2. Definition and use of functions 3. Parameters and variables of functions 4. Two dimensional arrays
Network - data link layer
DNS域名解析
Create, traverse and search nodes as required for single linked list
Method of designing test cases
Uniapp monitor whether the app has a network connection
远程访问云服务器上Neo4j等服务的本地网址
Ubuntu MySQL setting remote access permissions
多进程(多核运算)Multiprocessing
FTP service
MOOC翁恺 C语言 第三周:判断与循环:1.判断
MOOC翁恺C语言第七周:数组运算:1.数组运算2.搜索3.排序初步
shell脚本——正则表达式