Home  >  Article  >  Web Front-end  >  A closer look at non-blocking I/O in Node.js

A closer look at non-blocking I/O in Node.js

青灯夜游
青灯夜游forward
2021-07-02 10:48:111719browse

A closer look at non-blocking I/O in Node.js

Speaking of Node.js, you have probably heard a bunch of dizzying terms

About asynchronous, mainlyEvent loop and **Non-blocking I/O, **officially because of these two points, Node.js can be called high performance.

So it is very important to understand the asynchronous mechanism and usage of Node.js

What is the non-blocking I/O of Node.js

  • I/O is Input/Output, the input and output of a system
  • The difference between blocking I/O and non-blocking I/O lies in the time it takes for the system to receive input and then output. Can it Receiving other input

Example of queuing for rice

A closer look at non-blocking I/O in Node.js

The difference between blocking I/O and non-blocking I/O It depends on the time it takes for the system to receive input and then output, and whether it can receive other inputs

  • For customers/ordering staff

      Queuing up to order food is blocking I/O (customers at the back must wait for the customer in front to receive it before they can receive it)
    • Ordering food in the restaurant is non-blocking I/O (because there are many waiters in the restaurant, others order While serving, you can also order by yourself)
  • ##Compare the canteen aunt and waiter to the software system
  • The canteen aunt can only serve one portion Cooking on the floor -> Blocking I/O
    • The waiter can serve other guests after ordering -> Non-blocking I/O
  • Input = order food
  • Output = serve food
  • [Recommended learning: "
nodejs tutorial

"]

Examples of asking questions and waiting for solutions

For example: A developer raised a question on the forum: "How to solve this Node.js problem? Waiting online, urgent" .

The situation of blocking I/O is-> He keeps waiting for other people to answer, and when other people answer, he does other things.

The situation with non-blocking I/O is-> He goes to do other things, and then comes back to see the answer after a while

Example of doing housework

Xiaofang needs to do the following things to do housework:

Wash clothes in the washing machine (20 minutes))
  • Sweep the floor (10 minutes)
  • Tidy up Desk (10 minutes)
  • Hanging clothes (10 minutes)
  • Please design a clever and reasonable sequence so that Xiaofang can complete these things in the least amount of time

A: 20 minutes
  • B: 25 minutes
  • C: 30 minutes--because when the washing machine is washing clothes, Xiaofang can do other things✅
  • D : 35 minutes
Understanding non-blocking I/O

Notes:

Confirm
    system, enter, Output
  • , what are they? During the I/O process, can other I/O be performed?
Understand the blocking I/O from the code

const glob = require("glob");

var result = null;
console.time("glob");
// 获取目下的文件和文件名
result = glob.sync(__dirname + "/**/*");
console.timeEnd("glob");
console.log(result);
Print the following results

A closer look at non-blocking I/O in Node.jsYou can see that it takes

30 milliseconds of waiting time

to get the result It can be concluded from the above code that

Blocking I/O has a waiting time, and no other things can be performed during the execution process

Understanding non-blocking I/O from the code

const glob = require("glob");

var result = null;
console.time("glob");
// 获取目下的文件和文件名
glob(__dirname + "/**/*", function (err, res) {
  result = res;
  console.log("got result");
});
console.timeEnd("glob");

console.log(1 + 1);
Print the following results

glob: 3.198ms
2
got result

It can be concluded from the above code that

non-blocking I/O reduces the waiting time , you can also perform other things during the execution process

For more programming-related knowledge, please visit:

Introduction to Programming

! !

The above is the detailed content of A closer look at non-blocking I/O in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete