This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging

Written in the book of the former

I want to learn Node.js for a long time, I think the front-end must understand the server knowledge, and I can complete a small full stack project independently, but due to the time factor, I have no time to learn some basic knowledge last semester, just today’s second course passed, Node.js I will roll you again!!

  • I/O is input/output, a system’s input and output
  • Blocking I/O differs from non-blocking I/O in that the system receives input and can receive other input between output

For example, 🌰

  • Go to the canteen: we all have to queue for food

    Our process is: line up — — — — — – > in front of the person dozen rice — — — — — — — — > it’s our turn to their rice — — — — — — — > began to eat

  • Eating out: restaurant orders

    Now our procedure is: sit down ——-> order ——-> Wait ——–> start eating

    Try to start using some weird stuff in Markdown

  • Queuing for food vs. ordering food at a restaurant

  • For those of us who order:

    • Queuing for lunch is blocking I/O
    • Restaurant orders are non-blocking I/O

Moving on to the top sentence:

The receiving input of the system can receive other inputs during the output period

In chestnuts, system = the aunt or waiter in the canteen, input = order, output = serving (serving)

Canteen aunt can only a meal ———> blocking I/O

The waiter can serve other guests after ordering ——-> Non-blocking I/O

In fact, elementary school teachers taught us this question

Xiao Fang helps her mother do housework. She needs to do: wash clothes in the washing machine (20 minutes), sweep the floor (10 minutes), tidy the desk (10 minutes), hang clothes (5 minutes). Can you devise a clever new order so that xiao Fang can do these things in at least () minutes?

A.20

B.25

C.30

D.35

Didn’t you think? (I didn’t think of that either.)

In this process, we use the washing machine to wash clothes = input, airing clothes = output, while washing clothes in the washing machine, we can do other things, so this is non-blocking I/O yo.

thinking

What is the first thing to determine to understand non-blocking I/O?

  • We want to make sure we have an input/output system.
  • Consider whether other I/ OS can be performed during I/O.
    • Can ——> non-blocking
    • Cannot ——-> block

Write a chestnut 🌰

First create an index.js, then open our front-end person’s VS-code, open the terminal, run NPM install glob to install a glob package, to help us more convenient to observe whether I/O block.

Let’s start with blocking I/O

Paste the code: index.js first

const glob = require('glob');

var result = null;
console.time('glob')
result = glob.sync(__dirname + '/ / * * *')
console.timeEnd('glob')
console.log(result)

Copy the code

First, require introduces our glob package. Next, glob.sync does an operation to print the directory. While printing the result, use time/timeEnd to record the time and see how much time node takes to perform the operation.

Run the file directly from the terminal by typing node index.js

Look at the first line. On my computer, it took 20.93 milliseconds to execute, which is not a small amount for a server.

Now 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('Did you roll it today? ')
Copy the code

This time, we will use a callback function to print out ‘got result’ because there are too many lines of result. Instead, we will print out ‘got result’ and print out a statement after the timing is complete. Let’s see the result:

First of all, our time is 3.258ms, which is much less than the previous 20.93, not too much. After that, we output the statement, and then print the operation result we want. That is to say, it does other operations between the input and output, which has no impact on the result, and it takes much less time!

conclusion

My understanding 🐱🐉 : Non-blocking I/O allows us to reduce a lot of waiting time, and there are other things we can do while waiting.

Nothing is absolute. It doesn’t mean that non-blocking I/O is always good. Take a restaurant as an example. Blocking I/O Because there are multiple servers, one-to-one service, even if one of the accidents, will not affect the overall quality, and hire multiple servers will also pay the corresponding cost.