首页 > 语言 > JavaScript > 正文

浅谈Node异步编程的机制

2024-05-06 15:25:29
字体:
来源:转载
供稿:网友

本文介绍了Node异步编程,分享给大家,具体如下:

目前的异步编程主要解决方案有:

事件发布/订阅模式 Promise/Deferred模式 流程控制库

事件发布/订阅模式

Node自身提供了events模块,可以轻松实现事件的发布/订阅

//订阅emmiter.on("event1",function(message){  console.log(message);})//发布emmiter.emit("event1","I am mesaage!");

侦听器可以很灵活地添加和删除,使得事件和具体处理逻辑之间可以很轻松的关联和解耦

事件发布/订阅模式常常用来解耦业务逻辑,事件发布者无需关注订阅的侦听器如何实现业务逻辑,甚至不用关注有多少个侦听器存在,数据通过消息的方式可以很灵活的进行传递。

下面的HTTP就是典型的应用场景

var req = http.request(options,function(res){  res.on('data',function(chunk){    console.log('Body:'+ chunk);  })  res.on('end',function(){    //TODO  })})

如果一个事件添加了超过10个侦听器,将会得到一条警告,可以通过调用emmite.setMaxListeners(0)将这个限制去掉

继承events模块

var events = require('events');function Stream(){  events.EventEmiiter.call(this);}util.inherits(Stream,events.EventEmitter);

利用事件队列解决雪崩问题

所谓雪崩问题,就是在高访问量,大并发量的情况下缓存失效的情况,此时大量的请求同时融入数据库中,数据库无法同时承受如此大的查询请求,进而往前影响到网站整体的响应速度

解决方案:

var proxy = new events.EventEmitter();var status = "ready"; var seletc = function(callback){  proxy.once("selected",callback);//为每次请求订阅这个查询时间,推入事件回调函数队列  if(status === 'ready'){     status = 'pending';//设置状态为进行中以防止引起多次查询操作    db.select("SQL",function(results){      proxy.emit("selected",results); //查询操作完成后发布时间      status = 'ready';//重新定义为已准备状态    })  }}

多异步之间的协作方案

以上情况事件与侦听器的关系都是一对多的,但在异步编程中,也会出现事件与侦听器多对一的情况。

这里以渲染页面所需要的模板读取、数据读取和本地化资源读取为例简要介绍一下

var count = 0 ;var results = {};var done = function(key,value){  result[key] = value;  count++;  if(count === 3){    render(results);  }}fs.readFile(template_path,"utf8",function(err,template){  done('template',template)})db.query(sql,function(err,data){  done('data',data);})l10n.get(function(err,resources){  done('resources',resources)})

偏函数方案

var after = function(times,callback){  var count = 0, result = {};  return function(key,value){    results[key] = value;    count++;    if(count === times){      callback(results);    }  }}var done = after(times,render);var emitter = new events.Emitter();emitter.on('done',done);  //一个侦听器emitter.on('done',other);  //如果业务增长,可以完成多对多的方案fs.readFile(template_path,"utf8",function(err,template){  emitter.emit('done','template',template);})db.query(sql,function(err,data){  emitter.emit('done','data',data);})l10n.get(function(err,resources){  emitter.emit('done','resources',resources)})            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选