This article focuses on. two key concepts of js: non-blocking IO and event loops are summarized appropriately. If you need them, you can refer to the following concepts to learn and use Node. javascript has been writing a web application and a RESTful web api using express and mongoose for two months. js official website homepage for Node. introduction to js: Node. js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. so what does the non-blocking I/O model mean?
Non-blocking I/O model
First, IO operations are undoubtedly time-consuming. When the server receives a large number of requests, it creates a process or thread for each request and increases the memory overhead, it may also waste more time resources.
Because Node. js is event-driven, it uses an event loop to solve the bottleneck caused by IO operations. In Node. in js, an IO operation usually carries a callback function. When the IO operation is completed and returned, the callback function is called, and the main thread continues to execute the following code. An example is provided to illustrate this problem:
request('http://www.google.com', function(error, response, body) { console.log(body);}); console.log('Done!');
This code is used to send a request to 'HTTP: // www.google.com '. When the request returns this callback function, it outputs the response information. Due to the Node. js running mechanism, after this code is run, the 'done! ', And then output the response information after a period of time.
Event loop
Next, we will discuss the event loop mechanism. First, let's talk about calling pipeline. For example, there is the following code:
function A(arg, func){ var a = arg; func(); console.log('A'); } function B(){ console.log('B');} A(0, B);
After the code is executed, function A is first pushed into the calling sequence to become the top element of the stack and start to execute A. During the execution, function B is pushed into the calling sequence to become the top element of the stack, after B completes the execution, B is called the consumer, and A becomes the top element of the stack again. After A completes the execution, A is called the consumer, and the called consumer is idle.
There is a message queue in the Javascript runtime, and the message is associated with a callback function. When an event is triggered, if the event has a corresponding callback function, the message is added to the message queue.
Let's look back at what the event loop is. After the code starts to be executed, the function is pushed into the calling pipeline. In this example, the request is pushed into the calling pipeline, this function will make an http request (this http request will be sent to Node. the underlying module of js. log is pushed into the calling pipeline to start execution. When the request is complete, the completion event is triggered, and a message is added to the Message Queue. The message queue first checks whether the called consumer is idle. If the called consumer is not idle, after the consumer is idle, the header of the message queue is displayed. The callback function associated with the message is executed.
Summary
The above section summarizes the concept of the non-blocking model and event loop. This event Loop Mechanism is not just a Node. js exclusive, and Node. javaScript code is executed in a single thread. What are the advantages of js code in the face of a large number of concurrent requests?
The figure above shows Node. js architecture, Node. at the underlying layer of js, a module is responsible for maintaining the thread pool. When an IO request is sent, Node. the underlying module of js creates a thread to process the request, and then returns the result to the upper layer. So when there are multiple requests, Node. the underlying module of js will use as few threads as possible to complete the most tasks. If there are Idle threads, it will continue to be used for other tasks, this is undoubtedly a "smart" and more efficient process or thread for each request.
This article is a summary of how to learn Node. js. If you have any problems or deficiencies, please criticize and correct them.