Preface
In my image, asynchronous first appeared with ajax. I was still working on .net at that time, and then .net actually released an asynchronous control...
Although I finally knew that it was not asynchronous... Then, the front-end asynchronous is used a lot. If it is not an asynchronous program, you are embarrassed to say that the NodeJs you wrote was made by Opportunity JavaScript.
The feature of asynchronous programming model has also been brought over. Asynchronous has many advantages, but it is a nightmare for design. Asynchronous can disrupt the timing, so it increases the design difficulties.
However, asynchronous has revolutionized performance improvement and user experience, so the asynchronous features of NodeJS are quite obvious. Today we will learn it simply
Asynchronous I/O
In fact, at the operating system level, there are only two I/O methods, blocked and non-blocked
In the blockage model, the application needs to wait for the I/O to complete before returning the result. Its characteristic is that the back midfielder is called to wait for the system to complete all operations. This will cause the CPU to wait, rather than return immediately after the blockage call.
I was reading a book when I was a beginner, but I felt that it was not clearly described here. Moreover, asynchronous model is actually very large, just an improvement in the level of feeling. Let me give you a simple example
I now have a single-page application view of two single-page applications. When I search, I need to search through various channels. Shenzhen needs to call a third party, and the third party then obtains data from specific channels.
Of course it is very slow at this time. If I directly cut from A to B and B to load data, there will be no problem, but now the problem is that I need animation effects when switching from A to B.
This requires that the Bview rendering has ended during the switching process, and at least it will not be shipped to the process to obtain the data and start rendering. Therefore, asynchronous may not be so easy to use. Even if the data is requested asynchronously, the data must be obtained before the page can be loaded.
This is still blocked loading, there is no way to do this in business
No technology is perfect, blockage causes CPU waiting to be wasted, non-blocking disrupts logic, may also require polling to confirm whether the load is completed (I used to use polling to detect whether a dom was generated)
NodeJs adopts an event loop mechanism. When the process starts, Node will create a dead loop. Each process of executing the loop body is a Tick. The process of each Tick is to see if there are events that need to be processed.
If there is, remove the event related, execute it, and then enter the next logic. If there is no, exit the loop
During each Tick process, there are one or more observers in each event loop. The process of judging 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. The events we register will be placed in a "container" object. At this time, it is just a registration. These functions will be triggered after meeting the conditions (when the page changes), and the related events will be taken out from the container for execution.
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 doms and then trigger the callback functions of these doms.
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 to provide event production, events are passed to various observers, observers register events, and event loops are responsible for taking out events and then executing events
PS: Take click as an example. Each DOM observer first registers the event, the page process constantly monitors the page, the user clicks the page production event, and then the registered click event is taken out from the container and executed.
General function logic is controlled by us:
The code copy 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, the callback function is no longer controlled by the developer. Each time a js call is initiated, a transition product request object will be generated.
The code copy 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 obtain relevant data. The c++ related interface is called internally, and an intermediate object will be generated during the process, and all our states will be included...
PS: I don't feel good 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.