Today we will introduce node's event loop mechanism, which is a basic concept of some interactive systems and part of the core of JavaScript language. For users who use browsers, JavaScript processes users' requests by handling click, mouse, and other related events. For a server like node, the event loop also plays a very important role, for example, to process request from the client. Next we will give a brief introduction to the node event loop.
Node uses a single-threaded processing mechanism to handle all I/O requests in a non-congested way. That is to say, for HTTP requests, data queries, file I/O and other operations, the system will not block the current thread and wait for him until he returns the result. On the contrary, they will immediately execute the current request, send a callback event to process the results of the current request, and then process the new request. When the previous callback function returns the result, it outputs the result. This means that a large number of callback functions exist in node to process different requests. The callback function often initializes other callback functions in cascade mode, which is quite different from the browser's processing method. Node lacks concurrency. It only uses one CPU on the server, rather than multiple. The problem with using multiple CPUs is that it is necessary to constantly coordinate the operation between multiple CPUs, which leads to communication between different CPUs and mutual status information exchange, this is a complicated processing mode. Node uses a single thread to avoid this problem. This processing mechanism of node is more efficient. The following is an example.
Suppose we are going to eat at a fast food restaurant. There are two different service modes for fast food restaurants, one is event-driven (our node server), and the other is not (like IIS or Apache ). For a traditional server, after receiving your request, it will not receive the next user until it completes your request. After the waiter enters your order, there are still many things to do, process your payment, help you pour water, and wait for a while (not long) to wait for the kitchen to prepare your hamburger. A waiter (equivalent to a thread on the server) can receive only one customer at a time, and will not receive the next customer until the reception of the current customer is completed. Obviously, this method is not efficient, and he wasted too much time waiting for the kitchen to cook a hamburger. In reality, fast food restaurants adopt another mode. After receiving your order, they will give you a number card, which is equivalent to a callback function. Then he will receive the next customer. When your order is ready, the waiter will call your number to ask you to pick up the meal. This is the mode adopted by node. It can be seen that it is much more efficient.
Node is not suitable for scenarios with a long processing time for a single request. It is suitable for scenarios with a large number of concurrent requests and a short processing time for each request. Let's look at an example:
EE = require ('events'). eventemitter; EE = new EE (); Die = false;
Ee. On ('die', function () {die = true;}); setTimeout (function () {ee. emit ('die') ;}, 100 );
While (! Die ){}
Console. Log ('done ');
You can guess whether 'done' will be output. The answer is obvious. node has no chance to call back the timeout method,ProgramWill be blocked at the while loop. You can also execute a browser version:
VaR die = false; setTimeout (function () {die = true }, 100); While (! Die) {} alert ('done ');
The browser also does not pop up 'done '. This simple example can be used to explain the Javascript single-thread execution. Javascript will do other things only after the previous things are processed. As in the above example, JavaScript will always focus on processing the whileCode, There is no chance to execute the timeout method. This also explains why node is not applicable to scenarios where a single request takes a long time (for example, it takes a long time to process the content returned by the callback function ). Before processing the current request, node has no chance to receive the next request and process the callback of another request, which directly affects the experience of other users.
There are two suggestions for developing the node program:
1. Once code Initialization is complete, the event is activated;
2. If you encounter a time-consuming operation, you can delegate it to web worker for execution.
References:
Http://www.infoq.com/cn/articles/what-is-nodejs
Http://www.nodebeginner.org/index-zh-cn.html
Http://baike.baidu.com/view/3974030.htm? Sublemmaid = 3974030 & fromenter = nodejs