node-webkit中子進程的應用

node-webkit客戶端中應用本地數據庫若是大量寫入不少數據會致使主線程負責頁面渲染和監聽事件變慢。正如node介紹的那樣,像這種CPU運算密集型操做,最好放到子進程中來處理。注意的是主進程和子進程的通信與回調方法的執行。主進程與子進程是經過socket通信,因些傳遞的參數只能是PlanObject型(json型),不能把回調函數什麼的傳過去。node

主進程代碼:mian.jsweb

      //子進程路徑
      var childpath=require('path').join(__dirname,"../childprocess/childprocess.js");
      //生成子進程  
      var childprocess=require('child_process').fork(childpath);     
      //子進程運行事件監聽 
      childprocess.on("error",function(err,stdout,stderr){
        console.error("childprocess ERROR:",err,stdout,stderr);
      })
      // 異步回調
        function Receipt(){
          this.m={};
        }
        Receipt.prototype={
          push:function(id,method){
            this.m[id]=method;
          },
          invoke:function(id,args,scope){
            if(!this.m[id])return;
            this.m[id].apply(scope,args);
            delete this.m[id];
          },
          remove:function(id){
            if(this.m[id])
              delete this.m[id];
          }
        }
        //執行完消息的回調
        var receipt = new Receipt();
      /* 定義收到消息格式        
        {
        id:"sendedId",
        method:"childprocess method to call",
        err:"null or error"
        result:"sucess result"
        }
      */
      //收到消息監聽
      childprocess.on("message",function(msg){
        console.log("主進程收到消息",msg);
        if(msg&&msg.id){
           receipt.invoke(msg.id,msg); 
        }
      });
      /* 定義發送消息格式
        {
        method:childprocess.method,
        args:childprocess.method.arguments
        id:thisCalledUniqueId
        }
      */
      var getMyMothod={method:"myMethod",args:[{"anyPlanetObj":"anyPlanetObj"},"two arg"],id:require('node-uuid')()};
      receipt.push(getMyMothod.id,function(msg){
        console.log("getMyMehtod callback called",msg);
      });         
      childprocess.send(getMyMothod);

子進程代碼:/childprocess/childprocess.jssql

var childprocess={  
  test:function(feedbackid){
       if(process.send)process.send({id:feedbackid,method:"test",err:null,result:"test feedback ok"})
  },
  myMethod:function(sessoinObj,currentStr,feedbackid){
      try{
          /*your logic here*/
          if(process.send) process.send({id:feedbackid,method:arguments.callee.name,err:null,result:"your result data"});
      }cathe(e){
          if(process.send) process.send({id:feedbackid,method:arguments.callee.name,err:e,result:null});
      }
  }
}
/*傳入message消息的格式
{method:childprocess.method,
args:childprocess.method.arguments
id:thisCalledUnqueId}
*/
process.on("message",function(data){
  if(data&&data.method){
    data.args.push(data.id);   
    childprocess[data.method].apply(childprocess,data.args)
  }else{
    console.log("子進程","childprocess's message event no method!")
  }  
})
exports = module.exports = childprocess;

另外,若是是sqlite3在子進程中執行,在子進程中會缺乏引用,子進程執行是在node內核中執行,它找的node_sqlite3.node目錄就不同,在sqlite3的包中的binding中建立文件夾爲「node-v14-win32-ia32」再把node_sqlite3.node從別"node-webkit-v0.10.3-win32-ia32"等版本目錄中考過去就能夠正確執行了。數據庫

相關文章
相關標籤/搜索