nodejs-第二章-第二节-nodejs事件循环(2-2)

process.nextTick
  • process.nextTick()不在event loop的任何阶段执行,而是在各个阶段切换的中间执行;即从一个阶段切换到下个阶段前执行;
  • 三种定义异步事件的方式: setTimeout,setImmediate,process.nextTick()
var fs = require('fs');
fs.readFile(__dirname,() =>{
    setTimeout(() =>{
        console.log('setTimeout')
    })
    
    setImmediate(() =>{
        console.log('setImmediate')
        process.nextTick(() =>{
            console.log('nextTick3')
        })
    })
    
    process.nextTick(() =>{
            console.log('nextTick1')
    })
    
    process.nextTick(() =>{
            console.log('nextTick2')
    })
})
// nextTick1 nextTick2 setImmediate nextTick3 setTimeout
nextTick应用场景
  1. 在多个事件交叉执行cpu运算密集型的任务
var http = require('http');
function compute(){
    process.nextTick(compute)
}
http.createServer(function(req,res){
// 服务请求的时候,还能抽空进行一些计算任务;
    res.writeHead(200, {'Content-type': 'text/plain'})
    res.end('hello world');
})
compute()
  • 在这种模式下,咱们不须要递归的调用compute(),咱们只须要在事件循环中使用process.nextTict()定义;compute()在下一个事件点执行便可;在这个过程当中,若是有新的http请求进来,事件循环机制会先处理新的请求,而后再调用copute().反之,若是把compute()放在一个递归里调用,那系统一直会阻塞在compute()里,没法处理新的http请求了。
  1. 保持回调函数异步执行的原则
  • 当给一个函数定义一个回调函数时,要确保这个回调是异步执行的(定义一个callback,可是又须要在callback里面使用这个变量);
  • 下面示例违反了这一原则:
function asyncFake(data,callback){ // 同步执行
    if(data === 'foo') callback(true)
    else callback(false)
}
asyncFake('bar',function(result){
    // this callback is actually called synchronously!
})

为何这样很差呢?看下面nodejs文档里的一段代码node

var client = net.connect(8124, function(){
    console.log('client connect');
    client.write('world'); // 会报错
})

在上面的代码里,若是由于某种缘由,net.connect()变成同步执行的了,回调函数就会马上被执行,所以回调函数写到客户端的变量就用于不会被初始化了; 这种状况下咱们就能够用process.nextTick()把上面的asyncFake改为异步执行的;app

function asyncReal(data, callback){
    process.nextTick(function(){
        callback(data === 'foo')
    })
}
  1. 用在事件触发过程当中

EventEmitter 有两个比较核心的方法,on和Emit。node自带的发布/订阅模式;异步

var EventEmitter = require('events').EventEmmiter;
function StreamLibrary(resourceName){
    this.emit('start')
}
StreamLibrary.prototype.__proto__ = EventEmitter.prototype; // inherit from EventEmitter
var stream = new StreamLibrary('fooResouce');

stream.on('start', function(){
    console.log('Reading has started')
})

以上代码在new StreamLibrary的时候,已经触发了emit,此时 尚未订阅,console.log不会执行 解决方案以下:用异步方法包装async

function StreamLibrary(resource){
    var self = this;
    // 保证订阅在发布以前
    process.nextTick(function(){ 
        self.emit('start');
    })
// read from the file,and for every chunck read.do;
this.emit('data', chunkRead)
}

发布订阅模式函数

const EventEmitter = require('events').EventEmitter;
class App extends EventEmmiter{
    
}
let app = new App();

app.on('start',() =>{ // 订阅
    console.log('start');
})

app.emit('start') // emit 触发,emit是个同步的方法
console.log(111);  // 若是须要emit是异步的,能够经过三种异步方法去包装
// start 111
相关文章
相关标签/搜索