当前位置:网站首页>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
边栏推荐
- ---Stack & queue---
- Small turtle C (Chapter 5 loop control structure program 567) break and continue statements
- [learning notes] VIM editor
- [learning notes] tool
- About gcc:multiple definition of
- JSON notes
- DOM operation cases
- Technology sharing | how to do Assertion Verification in interface automated testing?
- 360 compatibility issues
- DOM - Events
猜你喜欢

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

How to describe a bug and the definition and life cycle of bug level

DHCP服务

Svg understanding and drawing application

主动扫描技术nmap详解

Clock tree analysis example

Custom component -- data listener

MySQL installation and use

MOOC翁恺 C语言 第三周:判断与循环:2.循环

RAID disk array
随机推荐
Esxi community network card driver
As a result, fill in the birthday candles
Neo4j运行报错Error occurred during initialization of VM Incompatible minimum and maximum heap sizes spec
DNS正向解析实验
VSphere esxi 7.0 update 3 release notes
YUM仓库的搭建
DNS域名解析服务
Icc2 analysis timing artifact analyze_ design_ violations
Result number of filled briquettes
小甲鱼C(第五章循环控制结构程序567)break和continue语句
MOOC Weng Kai C language week 5: 1. cycle control 2. multiple cycles 3. cycle application
Uniapp monitor whether the app has a network connection
RAID disk array
MOOC翁恺 C语言 第三周:判断与循环:1.判断
Applets: WSX scripts
Joern的代码使用-devign
raid磁盘阵列
[learning notes] process creation
Ubuntu MySQL setting remote access permissions
Clock tree analysis example