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:
- Depth 3rd asynchronous I/O-How does node. JS implement asynchronous I/O
- 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
- Open-source
- Written in C + +.
- Implement JavaScript based on the ECMA standard
- JavaScript can be compiled into machine code that can be recognized by the processor
- Can run independently
- 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?
- Managing Reusable Code
- Working with files
- Working with databases
- Internet communication
- Accept request, send respond
- Handle work that takes time to complete
node. JS Schema
- The first layer is C + + core, which is the new customized features (other explanations, such as event LOOP,LIBUV, etc.).
- 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)
- 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
- The JS core part calls the C + + core (one of the fs.js calls. Cc:github.com/nodejs/node ...)
- C + + Core call LIBUV
- 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.
- LIBUV send the encapsulated Request object to the OS
- Thread pool (thread pool) that is sent to the OS waits to be executed
- 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
- 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)
- Event loops out of the observer's queue in the completed state request object
- 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