Chapter 6 Node.js event loop

Node.js is a single-process single-threaded applications, but due to the asynchronous callback interfaces V8 engine provides, can handle a large number of concurrent Through these interfaces, so performance is very high.
Node.js API is supported by almost every callback function.
Node.js substantially all of the event mechanism is implemented in design mode observer mode.
Node.js is similar to entering a single thread while (true) event loop until there are no exit events observer, each asynchronous event generates an event observer, if an event occurs is called the callback function.

6-1 event-driven programming

Node.js event-driven model, when the web server receives the request, and then processed to put it off, and then the next request to a web service.

When the request is completed, it is placed back processing queue, when the queue reaches the beginning, the result is returned to the user.

This model is very efficient scalability is very strong, because the webserver been receiving requests without waiting for any read or write operation. (This is also known as event-driven, non-blocking IO or IO)

In the event-driven model, it will generate a main loop to monitor events, triggers a callback function when an event is detected.
3168340-bf4a213c7f7ab664.jpg
image

The entire event-driven process is so achievable, very simple. Somewhat similar to the observer pattern, the event is equivalent to a theme (Subject), and the processing functions on this event is equivalent to all registered observers (Observer).

Node.js has multiple built-in event, we can through the introduction of events module, and to bind and listen for events by instantiating EventEmitter class, the following examples:

// 引入 events 模块
var events = require('events');
// 创建 eventEmitter 对象
var eventEmitter = new events.EventEmitter();

The following procedures binding event handler:

// 绑定事件及事件的处理程序
eventEmitter.on('eventName', eventHandler);
我们可以通过程序触发事件:

// 触发事件
eventEmitter.emit('eventName');

Creating main.js file, the code is as follows:

/ 引入 events 模块
var events = require('events');
// 创建 eventEmitter 对象
var eventEmitter = new events.EventEmitter();
 
// 创建事件处理程序
var connectHandler = function connected() {
   console.log('连接成功。');
  
   // 触发 data_received 事件 
   eventEmitter.emit('data_received');
}
 
// 绑定 connection 事件处理程序
eventEmitter.on('connection', connectHandler);
 
// 使用匿名函数绑定 data_received 事件
eventEmitter.on('data_received', function(){
   console.log('数据接收成功。');
});
 
// 触发 connection 事件 
eventEmitter.emit('connection');
 
console.log("程序执行完毕。");

Let's execute the code above:

$ node main.js
连接成功。
数据接收成功。
程序执行完毕。
How Node application works?

In Node application function performs the asynchronous operation as the last parameter callback, the callback function receives an error object as the first parameter.
Let us re-look at the previous examples, create a input.txt, document reads as follows:

菜鸟教程官网地址:www.runoob.com

Creating main.js file, the code is as follows:

var fs = require("fs");

fs.readFile('input.txt', function (err, data) {
   if (err){
      console.log(err.stack);
      return;
   }
   console.log(data.toString());
});
console.log("程序执行完毕");

Program above fs.readFile () function to read a file is asynchronous. If an error occurs in the process of reading the file, the error err objects will output an error message.

If no error occurs, readFile skip output err object, the document is output by the callback function.

The above code is executed, execution results are as follows:

程序执行完毕
菜鸟教程官网地址:www.runoob.com

Next we delete input.txt file, the results are as follows:

程序执行完毕
Error: ENOENT, open 'input.txt'

Input.txt because the file does not exist, so the output of the error message.

Guess you like

Origin blog.csdn.net/weixin_33716557/article/details/90934148