The event loop in Node

The event loop in Node

The underlying language of Node is libuv, which uses the v8 engine to parse js scripts.
libuv is responsible for calling the interface API, handing over different tasks to different threads for processing, and then handing over the processing results to the v8 engine, and the v8 engine sends the processing results to the user.
Execution order of tasks in Node

  1. timers timers, execute the callback functions of setTimeout and setInterval that have been arranged
  2. pending callback: I/O callback whose execution is delayed until the next loop iteration
  3. idle, prepare: only used internally by the system
  4. poll: Retrieve new I/O events and execute I/O-related callbacks.
  5. check: Execute the setImmedate() callback function
  6. Closed callback function close callbacks: for example socket.on('close', ()=>{})

Node deals with the operating system, so idle/prepare/pending callback
can be regarded as 6 macro task queues in Node, regardless of whether the macro task in the browser is a queue, and each queue will clear the current one when it is executed. The callback task in the queue will go to the next queue.
Considering microtasks: process.nextTick and Promise microtasks, before each queue is executed, the nextTick queue will be cleared and then Promise.
process.nextTick > setImmediate > microtask
The execution order of setImmediate and setTimeout(0)
When we run it directly, we will see that setTimeout(0) is executed first and then setImmediate is executed.
The fact is: their order depends on the operating speed of the operating system. The program of setTimeout is definitely more complicated than setImmediate. He needs to create a timer, so the execution event of setTimeout is longer than setImmediate.

console.log('start');
let timer1 = setTimeout(() => {
    
    
    console.log(`settimeout 0`)
    process.nextTick(() => {
    
    
        console.log('settimeout process nextTick');
    })
    clearTimeout(timer1);
}, 0);
let timer2 = setTimeout(() => {
    
    
    console.log(`settimeout 300`)
    clearTimeout(timer2);
}, 300);
let interval = setInterval(() => {
    
    
    console.log(`setInterval`)
    clearInterval(interval);
}, 0);


process.nextTick(() => {
    
    
    console.log('process nextTick');
})
const fs = require('fs')
fs.readFile('./test.js', () => {
    
    
    console.log('poll');
    setImmediate(() => {
    
    
        console.log(`poll setImmediate`)
    });
})
setImmediate(() => {
    
    
    console.log(`setImmediate`);
});
new Promise(resolve => {
    
    
    console.log('promise start');
    resolve();
    console.log('promise end');
}).then(() =>{
    
    
    console.log('promise result');
})
console.log('end');
start
promise start
promise end
end
process nextTick
promise result
settimeout 0
setInterval
settimeout process nextTick
setImmediate
poll
poll setImmediate
settimeout 300

  1. print start
  2. timer1 joins the timers queue
  3. timer2 joins the timers queue
  4. interval join timers queue
  5. immediate joins the check queue
  6. process.nextTick joins the microtask queue
  7. fs joins the I/O queue
  8. Execute Promise, print promise start, promise end Add callback to asynchronous microtask queue
  9. print script end
  10. Start emptying the microtask queue
  11. Clear promise.nextTick callback
  12. Clear the promise callback queue
  13. Start emptying the macrotask queue
  14. Clear the timer, execute setTimeout(0), add process.nextTick to the microtask queue
  15. setTimeout(300) time is not up, continue to be placed in timers and not executed
  16. setintervalexecution
  17. The timers queue has been emptied, look for microtasks, if there is, execute process.nextTick
  18. Start to empty the poll queue. At this time, it is not time to execute the task. Poll waits and checks whether there are tasks in other queues.
  19. Due to the existence of the setImmediate callback, poll will wait, first wait for the execution of setImmediate to complete
  20. The execution time of the poll queue task is up, clear the task, print poll, add setImmediate to the check queue
  21. Clear the check queue and print poll setImmediate
  22. One cycle ends, start the next cycle
  23. Start emptying the timers queue
  24. At this point only setTimeout(300) is left

Guess you like

Origin blog.csdn.net/qq_41028148/article/details/125263071