nodejs多线程模块togg2的API中文翻译

# TAGG2(Threads A GoGo2) for Node.js [![Build Status](https://travis-ci.org/DoubleSpout/node-threads-a-gogo2.png?branch=master)](https://travis-ci.org/DoubleSpout/node-threads-a-gogo2)


对于Node.js提供异步本地模块、事件和/或延续传递风格的API为移动闭塞/长CPU绑定的任务节点的事件循环,JavaScript线程在后台并行运行,使用所有可用的CPU内核自动;从所有在一个单一节点的过程。


tagg2是thread_a_gogo模块的叉子,它需要Nodejs版> = 0.8 x,感谢Jorge Chamorro Bieling。


tagg2支持Windows,Linux,Mac.


## 安装模块


With [npm](http://npmjs.org/):


多线程gogo2模块支持Windows,Linux,Mac。


确定已经有, node-gyp has installed.


     npm install tagg2


源码安装:
     git clone https://github.com/DoubleSpout/node-threads-a-gogo2.git
     cd node-threads-a-gogo2
     node-gyp rebuild


在您的项目中包括模块:


     var tagg2 = require('tagg2');


## 简单的例子


     var tagg2 = require('tagg2'); //加载模块


     var th_func = function(){


      console.log('我在线程,我的文件路径是 :' + __dirname);
      //in thread you can do some cpu hard work,such as fibo.
  //在多线程里你可以让cpu为你多工作一些
   
      thread.end("线程结束"); 
      //when thread over, the string "thread over" will transfer to main nodejs thread.
  //当线程结束,字符串“线程”将转移到主要的Nodejs线程
  //当多线程结束时,这个字符串会显示到nodejs的结束位置
      
         }
       
         var thread = tagg2.create(th_func, function(err, res){
         //var thread = tagg2.create(th_func, {buffer:buf}, callback); 可将缓冲区转换为线程.
         
         if(err) throw(err);//线程出现一些错误


         console.log(res);//这将是打印 "thread over"


        thread.destroy();//make suer to destory the thread which you have created
 //销毁当前线程


     });




## 线程池
 tagg2模块支持线程池和处理池,这将创造第一,和做其他任何的努力。
 当你有太多的线程,你的程序可能会被内存,所以池将保护你
     var tagg2 = require('tagg2'); //加载模块


     var buf = new Buffer('tagg2 buffer'); //创建传输到线程的缓冲区


     var th_func = function(){

  console.log(thread.buffer.toString());
  //这将打印tagg2缓冲'.

  thread.end("thread over"); 
  //当线程结束,字符串“线程”将转移到主要的Nodejs线程.
  
     }
    
     var thread = tagg2.create({poolsize:10,dirname:__dirname});//创建一个大小为10的线程池.
   
         thread.pool(th_func, buf, function(err, res){
         //thread.pool(th_func,callback); 可能无法传输缓冲区到线程.


      if(err) throw(err);//线程出现一些错误


      console.log(res);//this will be print "thread over"


      thread.destroy();//摧毁整个线程池


     });
   


##API


##tagg2


  object, you can get tagg2 object from require('tagg2');


##tagg2-thread and child process


tagg2线程使用pthread创建工作者线程,或者创建一个线程池,设置选项。fastthread = false,切换到孩子的过程模型,你可以在它的API使用NodeJS,确保当线程和子进程的工作完成后,调用函数的线程。端([但]),然后回调将被称为,和线程将返回到池如果你设定了一个线程池


###tagg2.create(...)


  返回一个线程对象,线程对象有一些方法和属性.


     ex1. tagg2.create(thread_function)     


     ex2. tagg2.create(thread_function, callback)


     ex3. tagg2.create(thread_function, options, callback)


     ex4. tagg2.create(options) //to create a thread pool
    
###options
      
  buffer:您要传输到线程的缓冲区,默认是false,创建池可能不设置它.


  poolsize:线程池大小,设置false 不使用线程池。默认为false;


  fastthread:设置false,使用NodeJS的子进程中,你可以使用所有的NodeJS模块和API,设置为true,使用快速线,你只能在它内使用JS函数,但更更快的.default是true. 


  dirname:if you want to use require in thread, make sure set the correct dirname.you can also set it by created thread pool like this:
 //如果你想使用需要线程,确保设置正确的路径.你也可以放在创建的线程池这样
     var threadp1 = tagg2.create({poolsize:10,dirname:__dirname});//线程池中的线程都使用相同的路径 __dirname


  notice if fastthread is false,you have the real another node.js process,so you can do anything in it.
  //注意 如果fastthread设为false,你要有另外一个真实的node.js进程,你才能做你想做的事情


###callback(err,res) 
  
  每个回调都有相同的参数
  the first argument is error, if some error occur in the thread, the parameter will be not null;
  //第一个回调参数为错误信息,如果线程中发现错误,那么这个参数将不为空
  the second argument is result,when thread.end(result) run in the thread, the result will transfer to main thread and execute the callback; thread.end method see below;
  //第二个参数是返回值,当这个线程运行结束时,这个返回值将通过主线程传递给回调,线程结束的方法请看下面


   
###线程


  object, 你可以通过获得线程对象来执行线程 如: tagg2.create(...).


  if you set poolsize from tagg2.create(...),like tagg2.create({poolsize:10}),return thread pool object.
  //如果你设置了线程池大小 创建线程时返回线程线程对象进线程池


###thread.id  --不支持线程池
      
  the number mark the thread or child process.
  //这个数字标志线程 或者 子进程


###thread.destory([true]])


  destory the thread or thread pool.make sure to execute it when you don't need the thread or pool any more.
  //销毁线程 或 线程池 当然标志着需要执行结束的线程或线程池 
     ex1. thread.destory();//平滑的杀死线程


     ex2. thread.destory([true]); //kill the thread or child process immediately,like kill pid
 //立即杀死线程或者子进程 ,像linux下 kill pid一样 马上线束


  notice: if using pool, once thread.destory called,the pool will destory but not the thread, thread when work finished the thread will auto return to the pool.
  //注意:如果使用了线程池,一旦认为线程被销毁,那么线程池将被销毁而不是线程,线程完成工作时,线程将自动返回到池中      
      
###thread.pool(...)   --线程池


  put some job to the idle thread in the thread pool,if all the thread is working, the job will wait.
  //将一些作业放在线程池中的空闲线程,如果所有的线程都在工作,则任务将等待
  ex1. thread.pool(thread_func)
  
  ex2. thread.pool(thread_func, buffer)


  ex3. thread.pool(thread_func, callback)


  ex4. thread.pool(thread_func, buffer, callback)
      
###thread.totalThreads() --线程池
        
 返回线程此池中的线程数量


     ex1. thread.totalThreads();

###thread.idleThreads() --线程池
      
  returns the number of threads in this pool that are currently idle (sleeping)
  //返回当前线程池中的空闲(休眠)线程的数量
     ex1. thread.idleThreads();


###thread.pendingJobs() --线程池
          
  returns the number of jobs pending
  //返回挂起的线程数量


     ex1. thread.pendingJobs()


###in the thread or child-process
   //线程或子进程


  in every thread or child function,you can use some global objects below;
  //在每一个线程或子函数中,你可以使用下面的一些全局对象
###thread.id


  线程或子进程的编号.
  
###thread.buffer   --缓冲区对象
        
 object,save the buffer object which has sent from main thread.
 //对象,保存已从主线程发送的缓冲区对象
###thread.buffer.toString()


 返回的字符串;得到缓冲UTF-8字符串


     ex1. thread.buffer.toString();


###thread.nextTick(function)


 异步做一个工作


     ex1. thread.nextTick(function(){


      console.log("Asynchronous")


      });


###thread.end([prarm])


     ex1.thread.end(); return undefined result to main thread callback
      //返回未定义的结果到主线程回调;


     ex2.thread.end(reslut); return array or object or string to main thread callback;
    //返回数组或对象或字符串到主线程的回调
      
###console.log(param)
      
  打印数组或对象或字符串或数字等输入


     ex1. console.log("tagg2")       


###require(filepath) 


 load a js file,you can use global object to read or write the Variable in the require file
 //加载一个js文件,你可以使用全局对象读取或写入文件的要求变
 //加载一个JS文件,你可以使用全局变量来保存这个对象执行读写




     ex1. require("./tagg2_require.js"); //make sure tagg2_require.js has in the same dir with __dirname,
 //要确定tagg2_require.js就在同一个目录下面  当前目录下面__dirname
 
 支持如下: "../../" , "./user", "/user".


 notice if fastthread is true, the required file not support "moudle.export", please use global.xxxx to share the function or object.
 //注意:如果fastthread设置为true,那么将不支持moudle.export,请用global.xxx来使用你的函数或对象


 notice. in the fast thread, there is not a really node.js runtime env,you can't require node.js module,so don't do that 'var fs = require("fs");'
 //注意:在快速的线程里,没有真正的node.js运行环境,你不需要加载node.js模块,所以不必这样'var fs = require("fs");'


###global    --在线程的全局对象里


  save the global object in the thread like Browser's window.you can set or get some value from it.every thread has it's own global.
  在保存的全局对象的线程里像浏览器窗口,你可以设置或获取它的值,每个线程都是自己的全局


###__dirname
        
  the nodejs dir path which filedirname you called create the thread.the pool used the same dirname when you create the pool.
  //这filedirname的Nodejs dir路径创建线程池叫你使用相同的目录当你创建的池
###user fastthread:false


  set fastthread to false will use the slow thread, it fact is a real nodejs process,tagg2 use of child.fork() to achieve
  //设置fastthread为false,将使用慢线程,它事实上是一个真正的nodejs进程,tagg2使用的是子分支(fork())函数来实现的
      


  all in the thread1's object and functionally
  //所有线程里的对象和方法
      
  when you set fastthread false, you can use all the api of node.js,There is no limit,tagg2 also provided a process pool.
  //当你设定fastthread虚假,你可以使用所有节点的API。JS,没有限制,tagg2也提供了一个进程池
  //当你设置fastthread为false,你可以使用nodejs里面的所有api,这里没有限制 tagg2也提供一个线程池




##more

  see /example , /benchmark and /test for more useful code. do test please run node ./test/main_test.js
  //见/例,/基准和/测试更有用的代码。做测试,请运行/测试/ main_test.js节点
##future
  
  TAGG2 module has already in experiment,so you may not use it in production.TAGG2 will more stronger and feature-richer.
  //tagg2模块已经在实验,所以你可能不会使用它在production.tagg2会更强,功能更丰富

  //tagg2模块已经还处理实验里,所以你不要使用在生产线上,tagg2将有更多更多强的功能


注:有些地方是我自己理解翻译的,有些是靠工具翻译的,如果错误,请提出,我改正

未经同意,请不要转载




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值