This article is some personal understanding of nodejs in actual development and learning. It is now compiled for future reference. I would be honored if it could inspire you.
Non-blocking I/O
I/O: Input / Output, the input and output of a system.
A system can be understood as an individual, such as a person. When you speak, it is the output, and when you listen, it is the input.
The difference between blocking I/O and non-blocking I/O lies in whether the system can receive other input during the period from input to output.
The following are two examples to illustrate what blocking I/O and non-blocking I/O are:
1. Cooking
First of all, we need to determine the scope of a system. In this example, the canteen aunt and the waiter in the restaurant are regarded as a system. The input is ordering, and the output is serving food.
Then whether you can accept other people's orders between ordering and serving food, you can determine whether it is blocking I/O or non-blocking I/O.
As for the cafeteria aunt, when he is ordering, he cannot order for other students. Only after the student has finished ordering and served the dishes, can he accept the next student's order, so the cafeteria aunt is Blocking I/O.
For the restaurant waiter, he can serve the next guest after ordering and before the guest serves the dish, so the waiter has non-blocking I/O.
2. Doing housework
When you are doing laundry, you don’t need to wait next to the washing machine. You can go there at this time Sweep the floor and tidy the desk. When the desk is tidied and the clothes are washed, hang the clothes to dry. It only takes 25 minutes in total.
Washing clothes is actually a non-blocking I/O. You can do other things between putting the clothes in the washing machine and finishing the washing.
The reason why non-blocking I/O can improve performance is that it can save unnecessary waiting.
The key to understanding non-blocking I/O is:
- Determine a system boundary for I/O. This is very critical. If the system is expanded, as in the restaurant example above, if the system is expanded to the entire restaurant, then the chef will definitely be a blocking I/O.
- During the I/O process, can other I/O be performed?
Non-blocking I/O of nodejs
How is the non-blocking I/O of nodejs reflected? As mentioned earlier, an important point in understanding non-blocking I/O is to first determine a system boundary. The system boundary of node is main thread.
If the architecture diagram below is divided according to thread maintenance, the dotted line on the left is the nodejs thread, and the dotted line on the right is the c thread.
Now the nodejs thread needs to query the database. This is a typical I/O operation. It will not wait for the result of the I/O and continue to process other operations. It will distribute a large amount of computing power to other c threads for calculation.
Wait until the result comes out and return it to the nodejs thread. Before getting the result, the nodejs thread can also perform other I/O operations, so it is non-blocking.
nodejs thread is equivalent to the left part being the waiter and the c thread being the chef.
So, node's non-blocking I/O is completed by calling c's worker threads.
How to notify the nodejs thread when the c thread obtains the result? The answer isEvent driven.
Event-driven
Blocking: The process sleeps during I/O and waits for the I/O to complete before proceeding to the next step;
Non-blocking: The function returns immediately during I/O, and the process does not wait for the I/O to complete.
How to know the returned result, you need to use Event driven.
The so-called Event-driven can be understood as the same as the front-end click event. I first write a click event, but I don’t know when it will be triggered. Only when it is triggered, let the main thread execute the event. driver function.
This mode is also an observer mode, that is, I first listen to the event, and then execute it when it is triggered.
So how to implement event drive? The answer is Asynchronous Programming.
Asynchronous Programming
As mentioned above, nodejs has a large number of non-blocking I/O, so the results of non-blocking I/O need to be obtained through callback functions. This method of using callback functions is asynchronous programming. For example, the following code obtains results through a callback function:
glob(__dirname+'/**/*', (err, res) => { result = res console.log('get result') })
Callback function format specification
The first parameter of the nodejs callback function is error, and the subsequent parameters are the result. Why do you do that?
try { interview(function () { console.log('smile') }) } catch(err) { console.log('cry', err) } function interview(callback) { setTimeout(() => { if(Math.random() <p>After execution, it was not caught and the error was thrown globally, causing the entire nodejs program to crash. </p><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/image/244/886/980/1657110712466688.png?x-oss-process=image/resize,p_40" class="lazy" title="1657110712466688.png" alt="Summary and sharing to understand several key nodes of nodejs"></p><p> is not captured by try catch because setTimeout reopens the event loop. Every time an event loop is opened, a call stack context is regenerated. Try catch belongs to the previous event loop. When the callback function of setTimeout is executed, the call stack is different. There is no try catch in this new call stack, so this error is thrown globally and cannot be caught. For details, please refer to this article<a href="https://juejin.cn/post/6995749646366670855" target="_blank" title="https://juejin.cn/post/6995749646366670855">Problems when using asynchronous queues for try catch</a>. </p><p>So what should we do? Use the error as a parameter: </p><pre class="brush:php;toolbar:false">function interview(callback) { setTimeout(() => { if(Math.random() <p>But this is more troublesome, and it needs to be judged in the callback, so a mature convention is produced. The first parameter is err. If it does not exist, it means the execution is successful. . </p><pre class="brush:php;toolbar:false">function interview(callback) { setTimeout(() => { if(Math.random() <h3 id="strong-Asynchronous-process-control-strong"><strong>Asynchronous process control</strong></h3><p>The callback writing method of nodejs will not only bring about the callback area, but also bring <strong>Asynchronous process control</strong> problems. </p><p>Asynchronous process control mainly refers to how to handle concurrency logic when concurrency occurs. Still using the above example, if your colleague interviews two companies, he will not be interviewed by the third company until he successfully interviews two companies. So how to write this logic? It is necessary to add one variable count globally: </p><pre class="brush:php;toolbar:false">var count = 0 interview((err) => { if (err) { return } count++ if (count >= 2) { // 处理逻辑 } }) interview((err) => { if (err) { return } count++ if (count >= 2) { // 处理逻辑 } })
Writing like the above is very troublesome and ugly. Therefore, the writing methods of promise and async/await appeared later.
promise
The current event loop cannot get the result, but the future event loop will give you the result. It's very similar to what a scumbag would say.
promise is not only a scumbag, but also a state machine:
- pending
- fulfilled/resolved
- rejectd
const pro = new Promise((resolve, reject) => { setTimeout(() => { resolve('2') }, 200) }) console.log(pro) // 打印:Promise { <pending> }</pending>
then & .catch
- The promise in the resolved state will call the first then
- The promise in the rejected state will call The first catch
- Any reject state followed by a promise without .catch will cause a global error in the browser or node environment. uncaught represents an uncaught error.
Executing then or catch will return a new promise. The final state of the promise is determined by the execution results of the callback functions of then and catch. :
- If the callback function is always throw new Error, the promise is in the rejected state
- If the callback function is always return, the promise is in the resolved state
- But if The callback function always returns a promise, which promise will be consistent with the promise status returned by the callback function.
function interview() { return new Promise((resolve, reject) => { setTimeout(() => { if (Math.random() > 0.5) { resolve('success') } else { reject(new Error('fail')) } }) }) } var promise = interview() var promise1 = promise.then(() => { return new Promise((resolve, reject) => { setTimeout(() => { resolve('accept') }, 400) }) })
The status of promise1 is determined by the status of the promise in return, that is, the status of promise1 after the promise in return is executed. What are the benefits of this? This can solve the problem of callback hell.
var promise = interview() .then(() => { return interview() }) .then(() => { return interview() }) .then(() => { return interview() }) .catch(e => { console.log(e) })
thenIf the status of the returned promise is rejected, then the first catch will be called, and the subsequent then will not be called. Remember: rejected calls the first catch, and resolved calls the first then.
promise solves asynchronous process control
If promise is just to solve the hell callback, it is too small to underestimate promise. The main function of promise is to solve asynchronous process control problem. If you want to interview two companies at the same time:
function interview() { return new Promise((resolve, reject) => { setTimeout(() => { if (Math.random() > 0.5) { resolve('success') } else { reject(new Error('fail')) } }) }) } promise .all([interview(), interview()]) .then(() => { console.log('smile') }) // 如果有一家公司rejected,就catch .catch(() => { console.log('cry') })
async/await
What exactly is sync/await:
console.log(async function() { return 4 }) console.log(function() { return new Promise((resolve, reject) => { resolve(4) }) })
The printed results are the same, In other words, async/await is just syntax sugar for promise.
We know that try catch captures errors depends on the call stack, and can only capture errors above the call stack. But if you use await, you can catch errors in all functions in the call stack. Even if the error is thrown in the call stack of another event loop, such as setTimeout.
Transform the interview code, you can see that the code is much streamlined.
try { await interview(1) await interview(2) await interview(2) } catch(e => { console.log(e) })
What if it is a parallel task?
await Promise.all([interview(1), interview(2)])
Event loop
Because of the non-blocking I/0 of nodejs, it is necessary to use the event-driven method to obtain the I/O results and achieve event-driven results. Asynchronous programming must be used, such as callback functions. So how to execute these callback functions in order to obtain the results? Then you need to use an event loop.
The event loop is the key foundation for realizing the non-blocking I/O function of nodejs. Non-blocking I/O and event loop are both capabilities provided by the libuv
c library.
代码演示:
const eventloop = { queue: [], loop() { while(this.queue.length) { const callback = this.queue.shift() callback() } setTimeout(this.loop.bind(this), 50) }, add(callback) { this.queue.push(callback) } } eventloop.loop() setTimeout(() => { eventloop.add(() => { console.log('1') }) }, 500) setTimeout(() => { eventloop.add(() => { console.log('2') }) }, 800)
setTimeout(this.loop.bind(this), 50)
保证了50ms就会去看队列中是否有回调,如果有就去执行。这样就形成了一个事件循环。
当然实际的事件要复杂的多,队列也不止一个,比如有一个文件操作对列,一个时间对列。
const eventloop = { queue: [], fsQueue: [], timerQueue: [], loop() { while(this.queue.length) { const callback = this.queue.shift() callback() } this.fsQueue.forEach(callback => { if (done) { callback() } }) setTimeout(this.loop.bind(this), 50) }, add(callback) { this.queue.push(callback) } }
总结
首先我们弄清楚了什么是非阻塞I/O,即遇到I/O立刻跳过执行后面的任务,不会等待I/O的结果。当I/O处理好了之后就会调用我们注册的事件处理函数,这就叫事件驱动。实现事件驱动就必须要用异步编程,异步编程是nodejs中最重要的环节,它从回调函数到promise,最后到async/await(使用同步的方法写异步逻辑)。
更多node相关知识,请访问:nodejs 教程!
The above is the detailed content of Summary and sharing to understand several key nodes of nodejs. For more information, please follow other related articles on the PHP Chinese website!

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor