Nodejs tutorial-asynchronous I/O_node.js

Source: Internet
Author: User
One of the core of nodejs is non-blocking asynchronous IO, so I want to know how it is implemented. After a study, I will find some answers and share them with you. Preface

In my image, Asynchronization first appeared with ajax. At that time, I was still engaged in. net, and then. net actually produced an asynchronous control ......

Although I finally realized that it is not asynchronous ...... then, the frontend uses a lot of asynchronous applications. If it is not an asynchronous program, you are embarrassed to say that NodeJs you write is an opportunity for javascript to be developed,

This feature of asynchronous programming model has also been introduced. asynchronous programming has many advantages, but it is a nightmare for design. asynchronous programming will disrupt the timing, which makes the design more difficult,

However, Asynchronization improves the performance and user experience. Therefore, NodeJS's asynchronous features are quite obvious. Today we will take a simple look.

Asynchronous I/O

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

In the congestion model, the application needs to wait for I/O to complete before returning the result. It is characterized by calling the Back Waist and waiting for the system to complete all the operations. This will cause the CPU to wait, the system will return immediately after the call is not blocked.

I read a book for beginners, but I don't think it is clearly described here. In addition, the asynchronous model is actually a great improvement in experience level. Here is a simple example.

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

This is of course very slow. If I directly switch from A to B and create A loading box in B, there is no problem loading data, but now the problem is that I need an animation when I switch from A to B.

This requires that the Bview rendering has ended when switching, at least not to get the data in the process to start rendering, so asynchronous may not be so easy at this time, that is, asynchronous request data, the page can be loaded only when data is obtained.

This is still blocking loading, and there is no way to do this in the business.

Any technology is not perfect. Blocking leads to a waste of CPU waiting. The logic is not congested. Don't say it may need to be round-robin to confirm whether the load is complete (I used round-robin to check whether a dom is generated)

NodeJs uses an event Loop Mechanism. When a process is started, Node creates an endless loop, and every time a cyclic body is executed, it is a Tick, the process of each Tick is to check whether there is any event to be handled.

If there is an event, extract the Event-related information, execute it, and enter the next logic. If there is no event, exit the loop.

In each Tick process, each event cycle contains one or more observers. The process of judging whether an event is to be handled is to ask the observers whether the event needs to be handled.

Take our html event model as an example.

For html, each of its DOM is actually an observer. The DOM of the Page observes the changes of our Web Page. After we provide an addEventListener for a DOM, a callback function will be registered for it, and the events we register will be put into a "Container" object. At this time, the callback function is only registered, these functions are triggered when the conditions are met (when the page changes), and related events are retrieved and executed from the container.

Now we click the previous point on the page, and then we will retrieve the click event set from the container. We will find the relevant dom and then trigger the callback functions of these dom.

Events may come from users' clicks or data changes. In Node, events mainly come from network requests, file I/O, and these events will all 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, the event is transmitted to various observers, the observer registers the event, the event loop is responsible for extracting the event and then executing the event

PS: Taking click as an example, each DOM observer registers the event first, and the page process continuously monitors the page. the user clicks the page production event, and then the registered click event is retrieved and executed by the container,

The general function logic is controlled by us:

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 );
}
}

Callback functions are not controlled by developers in asynchronous mode. Each js call will generate a transitional product request object.

The Code is as follows:


Fs. open = function (path, flags, mode, callback ){
Bingding. open (pathModule. _ makeLong (path), stringToFlags (flags), mode, callback );
};

Fs. open a file based on the path and parameters to obtain relevant data. The c ++ interface is called internally, and an intermediate object is generated during the process, all our statuses will be in ......

PS: I feel bad after reading it for so long.

Conclusion

The above is all about asynchronous I/O in nodejs. In my personal summary, if you have any omissions or errors, please point them out.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.