Home  >  Article  >  Web Front-end  >  nodejs tutorial asynchronous I/O_node.js

nodejs tutorial asynchronous I/O_node.js

WBOY
WBOYOriginal
2016-05-16 16:30:461426browse

Foreword

In my image, asynchrony first appeared with ajax. I was still working on .net at that time, and then .net actually came out with an asynchronous control...

Although I finally found out that it is not asynchronous... Then, the front-end is used a lot of asynchronous. If it is not an asynchronous program, you would be embarrassed to say that the NodeJs you wrote was made by JavaScript.

The feature of asynchronous programming model has also been brought over. Asynchronous has many advantages, but it is a nightmare for design. Asynchronous will disrupt the timing, so it increases the difficulty of design,

But asynchrony has revolutionized performance improvement and user experience, so the asynchronous features of NodeJS are quite obvious. Today we will learn briefly

Asynchronous I/O

In fact, at the operating system level, there are only two I/O methods, blocking and non-blocking

In the blocking model, the application needs to wait for the I/O to be completed before returning the result. Its characteristic is that it calls the backend and waits for the system to complete all operations. This will cause the CPU to wait, while non-blocking calls will return immediately.

I first read a book, but I feel that the description here is not clear, and the asynchronous model is actually very big, just an improvement in the level of perception. Let me give a simple example

I now have two single-page application views, a search page and a list page. When I search, I need to search through various channels. Shenzhen needs to call a third party, and the third party obtains data from specific channels

Of course it is very slow at this time. If I switch directly from A to B and then create a loading box or something to load data in B, there will be no problem. But the problem now is that I need animation effects to switch from A to B

This requires that Bview rendering has ended when switching, at least it will not get data in the process and start rendering, so asynchronous may not be so easy to use at this time. Even if it requests data asynchronously, it also requires data acquisition to load the page.

This is still blocking loading, and there is no way to do this in terms of business

No technology is perfect. Blocking causes a waste of CPU waiting. Not only does non-blocking disrupt logic, it may also require polling to confirm whether the loading is completed (I once used polling to detect whether a DOM was generated)

NodeJs uses an event loop mechanism. When the process starts, Node will create an infinite loop. Each time the loop body is executed, the process is a Tick. The process of each Tick is to see if there are events that need to be processed

If there is, take out the relevant event, execute it, and then enter the next logic. If not, exit the loop

In each Tick process, there are one or more observers in each event loop. The process of determining whether there is an event to be processed is to ask these observers whether the event needs to be processed

Take our html event model as an example

For HTML, in fact, each of its DOM is an observer. The DOM of the page observes the changes of our Web Page. After we provide an addEventListener to a DOM, we will register a callback function for it. The events we register will be put into a "container" object. At this time, it is just registration. These functions will be triggered after the conditions are met (when the page changes), and the relevant events will be taken out of the container and executed

We now click on a point on the page once, and then we will take out the click event collection from the container, we will find the relevant DOM, and then trigger the callback functions of these DOM

Events may come from user clicks or data changes. In Node, events mainly come from network requests and file I/O. These events will have corresponding observers, such as file observers and network observers

This is also a typical production/consumption model, asynchronous I/O, network requests provide event production, events are delivered to various observers, observers register events, and the event loop is responsible for taking out events and then executing them

PS: Taking click as an example, each DOM observer first registers the event, the page process continuously monitors the page, the user clicks the page to generate events, and then the registered click event is taken out from the container and executed,

General function logic is controlled by us:

Copy code The code is as follows:

var forEach = function (list, callback) {
for (var i = 0, len = list.length; i < len; i ) {
          callback(list[i], i, list);
}
}

In the case of asynchronous operation, the callback function is not controlled by the developer. Each time js is called, a transition product request object will be generated

Copy code The code is as follows:

fs.open = function (path, flags, mode, callback) {
bingding.open(pathModule._makeLong(path), stringToFlags(flags), mode, callback);
};

fs.open opens a file based on the path and parameters to get relevant data. It calls the c related interface internally, and an intermediate object will be generated in the process, in which all our states will be...

PS: I feel bad after watching it for so long

Conclusion

The above is all about asynchronous I/O in nodejs. Personal summary. If there are any omissions or errors, please point them out.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn