Home > Article > Web Front-end > What is non-blocking I/O in Node.js? Understand with examples!
Node.jsWhat is non-blocking I/O? This article uses examples to help you understand non-blocking I/O in Node.js. I hope it will be helpful to you!
Written at the front
I have wanted to learn Node.js for a long time. I feel that the front-end must understand server-side knowledge and be able to be independent. I completed a small full-stack project, but due to time factors, after learning some basics last semester, I no longer had time to learn. It just so happened that I passed the second course today, and I’m here to test you again with Node.js! ! ! [Recommended learning: "nodejs Tutorial"]
Go to the canteen to eat: we all have to queue up to get food
Our process is: queue up ------>wait for the person in front to get food--- ----->It's our turn to prepare our own food------->Start eating
Eating out: ordering food in the restaurant
Now we The process is: Sit down-------> Order-------> Wait--------> Start eating
Try to start Use some weird things in markdown
Queue to order food vs. Restaurant ordering
For those of us who order food For example:
Continue Let’s look at the sentence at the top:
The system receives input. During the output period, can it receive other input?
In Lizi, the system = cooking in the canteen The aunt or restaurant waiter, input = order, output = serve (serve food)
The cafeteria aunt can only serve rice one by one---------> Blocked I/O
The waiter can also serve other guests after ordering ------->Non-blocking I/O
Xiaofang helps her mother with housework. She needs to do: wash clothes in the washing machine (20 minutes), sweep the floor (10 minutes), tidy the desk (10 minutes), and hang clothes (5 minutes) ). Can you design a clever and reasonable new sequence so that Xiaofang can complete these things in at least ( ) minutes?
A.20
B.25
C.30
D.35
Didn’t expect that? (Actually, I didn’t expect it~)
In this process, we use the washing machine to wash clothes = input, and dry the clothes = output. While the washing machine is washing clothes, we can do other things, so This is non-blocking I/O.
To understand non-blocking I/O, what should we first determine?
First create a new index.js
, then open the vs-code of our front-end person, open the terminal, and execute npm install glob
Install a glob package to help us more easily observe whether I/O is blocked.
Let’s first look at blocking I/O
The code is pasted first: index.js
const glob = require('glob'); var result = null; console.time('glob') result = glob.sync(__dirname + '/**/*') console.timeEnd('glob') console.log(result)
First, use require to introduce our glob package , Next, use glob.sync to do an operation of printing the directory. While printing the result, use time/timeEnd to record the time and see how much time it takes for the node to perform this operation.
Enter in the terminal node index.js
Run this file directly
Look at the first line and execute it on my computer It takes 20.93 milliseconds. This amount is not small for a server.
Look at non-blocking I/O
Go directly to the code:
const glob = require('glob'); var result = null; console.time('glob') glob(__dirname + '/**/*',function(err,res){ result = res; // console.log(result) console.log('got result'); }) console.timeEnd('glob') console.log('今天你卷了没?')
This time, a callback function is used to operate, because the result is printed out too much. Okay, let's replace it with printing out 'got result' and perform a print statement after the timing is completed. Let's take a look at the result:
First It’s still our time: 3.258ms. Compared with the previous 20.93, it’s simply less, don’t be too much. At the end is the statement we output, and finally the operation result we want is printed. In other words, it is input Other operations were performed between the outputs, which had no impact on the results, and took much less time!
My understanding: Non-blocking I/O allows us to reduce a lot of waiting time, and during the waiting time, we can also perform some other tasks Operation (Welcome to give advice!!!)
There are no absolutes. It does not mean that non-blocking I/O is necessarily good. Let’s take a restaurant as an example. For example, if an accident occurs among the waiters, all the guests will have to wait for this waiter, which will affect the restaurant. Overall quality (can be understood as server crash); blocking I/O. Since there are multiple waiters, one-on-one service, even if one of them has an accident, it will not affect the overall quality, and hiring multiple waiters will require corresponding efforts. cost.
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of What is non-blocking I/O in Node.js? Understand with examples!. For more information, please follow other related articles on the PHP Chinese website!