2nd week of re-awareness with node. JS-node. JS Bottom

Source: Internet
Author: User

My 2nd week with node. JS-node. JS Bottom Book last time: "My first week with node. JS-node. JS style features." This time I read some things about the underlying implementation:
    1. Depth 3rd asynchronous I/O-How does node. JS implement asynchronous I/O
    2. Udemy "Learn and understand NodeJS learn and understand NodeJS" section 2&3

V8 engine

First of all, learning about node. JS must understand the V8 engine, a thing that can compile JS directly into a machine code (which the processor can recognize).

In more detail, V8 is a
    1. Open-source
    2. Written in C + +.
    3. Implement JavaScript based on the ECMA standard
    4. JavaScript can be compiled into machine code that can be recognized by the processor
    5. Can run independently
    6. Other C + + applications can also be embedded
JavaScript engine. node. js and V8 engine normal youth use V8: Run V8, JavaScript-v8-compile-> machine code literary youth has such a bold idea: those ordinary JS method is too boring, the ability to first. If I could write some C + + code as add-on to V8, it would be even more powerful if V8 had the ability to identify more JavaScript commands. For example file related things, originally JS can not do, now I use C + + at the bottom to achieve good, and then tell V8, when the user wrote in JS file.open (XXX), to use C + + to execute the function of file open, So your JS (b) is the ability to handle the file JS (Cow b). The idea of a literary youth is actually our node. js: A C + + application that inserts a V8 engine into it, and this C + + application implements super-many customized new features that make this application (node. js) very suitable for server development. What new features are needed for server development? = = = What are the aspects of the new functionality that node. JS implements?
    1. Managing Reusable Code
    2. Working with files
    3. Working with databases
    4. Internet communication
    5. Accept request, send respond
    6. Handle work that takes time to complete

node. JS Schema
    1. The first layer is C + + core, which is the new customized features (other explanations, such as event LOOP,LIBUV, etc.).
    2. The second layer is JS core, this layer with JS implementation, based on/call C + + core, so that users can better use those C + + features, but also implemented a lot of common functions.
In the node. JS source Code, C + + core is within the SRC folder. JS Core is within the Lib folder. This shows the hierarchical relationship between the two. node. js asynchronous I/O

node. js How to implement asynchronous I/O, from JS to the OS exactly what happens (this is the udemy+ "depth" of the Fit version summary)

    1. The JS code you wrote called the JS core of node. JS (such as the FS-related feature: Github.com/nodejs/node ...), and you set a callback function
    2. The JS core part calls the C + + core (one of the fs.js calls. Cc:github.com/nodejs/node ...)
    3. C + + Core call LIBUV
    4. C + + Core calls Libuv's method to encapsulate the request object (an important intermediate product in the asynchronous I/O process, between JS and the OS), which contains one of the most important things in asynchronous I/O: the callback function.
    5. LIBUV send the encapsulated Request object to the OS
    6. Thread pool (thread pool) that is sent to the OS waits to be executed
    7. The Req->result property of the Request object is the result of a thread in the threads pool that executes the I/O operations that are contained in the request object that is being sent. Then submit the completion status, which is similar to the notice saying "I'm done!" "And then return the thread to the threads pool
    8. The requested object in the completed state, the Observer (the two little people in the figure, different types of events have different observers) in the event loop (Events loop: A large while loop, each loop called a tick) is proposed (through the Libuv method to check whether there is a completed request), Then put it in the queue (completed Events queue)
    9. Event loops out of the observer's queue in the completed state request object
    10. Take out the results of the I/O operations contained therein and the callback function to execute in V8. This achieves the purpose of invoking the callback function set in # # #.
Note: A. The JavaScript that the V8 engine executes is synchronous (sync) (STACKOVERFLOW.COM/QUESTIONS/2 ... ) and single thread B. #1-#9 with the #10 is at work at the same time, the I/O event one executes, and then finally sends to the V8 engine one (because JS is synchronous) executes the callback function C. Because of B, the entire node. JS has the ability to asynchronous I/O d. The characteristics of event-driven (driven), non-blocking (non-blocking) I/O can also be explained. Event-driven refers to the #7-#10, where events are completed to trigger subsequent steps until the last callback function is executed. nonblocking I/O is the entire #1-#10这个过程, and the JavaScript code we execute in V8 does not stop because of the execution of the event, #1-#9和 # # work simultaneously, an I/O event, and a notification execution callback function. E. From the interpretation of D, it is possible to better understand the last paragraph in the previous article that node. js is single-threaded/multithreaded. Something related to the next write event and event Emitter. Welcome everyone to communicate, correct me ~

2nd week of re-awareness with node. JS-node. JS Bottom

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.