muti-thread
A framework let node support multi threads
Last updated a month ago by youjia .
Apache-2.0 · Repository · Bugs · Original npm · Tarball
$ cnpm install muti-thread 
SYNC missed versions from official npm registry.
-----------------------------✈--------------------------
   |-------------------------✈--------------------------
   |-------------------------✈--------------------------
   |-------------------------✈--------------------------

A framework let node support multi threads

Getting Started

This section contains guides for understanding and mastering features that this module offers.

If we let fast-diff run in a thread, we should

1 npm install fast-diff

2 create export.js

'use strict';

const fastDiff = require('fast-diff');

(function($) {
  $.fastDiff = function(good, bad, position) {
    var r;
    if(arguments.length === 2) {
      r = fastDiff(good, bad);
    } else {
      r = fastDiff(good, bad, position);
    }
    return JSON.stringify(r);
  }
})(global);

3 create webpack.config.js

'use strict';

var webpack = require('webpack');
var path = require('path');

module.exports = {
  target: 'web',
  entry:  {
    app: './export.js',
  },
  output: {
    path: path.join(__dirname, 'out'),
    filename: 'export.js'
  },
  resolve: {
    extensions: ['.js', '.json'],
    modules: [
      'node_modules'
    ],
  },
  externals:{},
  module: {
    loaders: [
    ]
  }
};

4 webpack --config webpack.config.js

5 the output file can be used

'use strict'

const co = require('co')
const Thread = require('muti-thread').Thread
const randomstring = require('randomstring')
const path = require('path')

// create a new thread
var thread = new Thread(path.join(__dirname, 'test', 'out', 'export.js'))

co(function* () {
  for(;;) {
    var good = randomstring.generate(1000)
    var bad = randomstring.generate(1000)
    var r = yield thread.execute('fastDiff', new Buffer(good), new Buffer(bad))
  }
})

Platform Support

OS X, Windows and Linux

Node engine

>=4.0.0

Cpu architectures

Not support arm , mips

Installation

Unix

  GCC 4.9.4 or newer
  Clang 3.4.2 or newer
  Python 2.6 or 2.7

Windows

  Building native add-ons: Visual Studio 2013 or Visual C++ Build Tools 2015 or newer
  Python 2.6 or 2.7

Once you have Node.js installed:

npm install -g node-gyp
npm install muti-thread

OS X

xcodebuild

Having installation troubles? Check out Supported toolchains

APIs

close 通知线程对象管理的线程退出,即从线程的消息循环中退出,结束线程的运行。线程对象以后不再使用,应该调用这个函数,及时的释放系统资源。如果没有调用这个函数,而线程对象满足 V8 垃圾回收的条件被回收的时候,会自动释放底层资源。因此,这个函数的调用不是必须的。但我们建议,由于线程是系统资源,如果确定不再使用,应该手动调用 close 及时释放底层资源。

isRunning 判断这个线程对象是否存在可用线程。线程对象创建成功后,内部的线程已经就绪,调用这个函数返回 true。close 函数调用之后,isRunning 返回 false。

numOfTasks 交给线程对象执行的任务队列中的个数。即排队中的,还没来得及执行的任务个数。程序中可以创建多个线程对象,可以用这个函数做负载均衡,将任务交给 numOfTasks 个数最小的线程对象。

execute 让线程对象执行一个函数,每个函数就是一个任务,线程对象内部有一个消息队列,抛给线程对象执行的任务以队列的方式按序执行。这样的函数,execute 支持向它传递 [0, 8] 个参数,参数类型为 Buffer,如果是字符串,则自动转为 Buffer。

A simple test report

License

Apache-2.0

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 241
Dependencies (4)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |