0.9.10 • Published 6 years ago

webworkers-helper v0.9.10

Weekly downloads
1
License
CPOL
Repository
-
Last release
6 years ago

WebWorkers-Helper

A simple Node.js webworker-threads module helper.

Note #1 This module can be used with any existing version of node.js and npm.

How to install the module:

By using npmjs.org:

 npm install webworkers-helper --global --save 

 npm install webworkers-helper --save 

How to use the webworkers-helper module installed:

Creating and running a pool of n webworker-threads

new WorkersHelper().createPool(3, [[ test_cb1, [[1,2,3]] ], 
               [ (thread, args) => { return self.thread.id; }, [[3,4,5]] ], 
               [ function (thread, args) { var f = (p1,p2) => { return p1 + p2; }; f(10,20); return 'oops tid = ' + self.thread.id; }, [[5,6,7]] ]]).then(function (ret) { console.log("Completed"); console.log(JSON.stringify(ret)); });

Constructing and running three-dimensional grid of webworker-threads

var callback3d = [ [  
      [ [test_cb1,[[1,2,3]]], [test_cb1,[[4,5,6]]] ],
      [ [test_cb1,[[7,8,9]]], [test_cb1,[[10,11,12]]] ] ],

                  [
      [ [test_cb1,[[13,14,15]]], [test_cb1,[[16,17,18]]] ],
      [ [test_cb1,[[19,20,21]]], [test_cb1,[[22,23,24]]] ] ]
];

new WorkersHelper().createGrid3d({ "dim_x" : 2, "dim_y" : 2, "dim_z" : 2 }, callback3d).
    then(function (ret) { 

        for (var x = 0; x < 2; x++)
            for (var y = 0; y < 2; y++)
                for (var z = 0; z < 2; z++)
                    console.log(ret[x][y][z]);

        console.log("Completed3d");
    }, console.error);

...

# Constructor

`.ArgvParser`
#####.WorkersHelper()
WorkersHelper - constructs a webworkers helper object,

# Methods

##### .createPool( n, args ).then(function (ret) { ... })

Creating a pool of n webworker-threads. The first parameter of this method `n` is the actual number of threads in a team.
The second parameter `args` is an array of arguments. Each argument in the array is a tuple of two values for each 
particular thread to be executed. This tuple contains two values of a thread callback function and an array of arguments
for a specific running thread. `createPool` is the promise-based method that invokes `then` method's callback function at
the end of its execution. The parameter variable `ret` is normally an array of items, each one containing a return value of
a specific thread in a team.

##### .createGrid3d( dim3d, callbacks ).then(function (ret) { ... });

Creates a three-dimensional grid of threads to optimize the performance of solving a multidimensional problem.
The first argument of this function is an object having the following components `{ "dim_x" : n_x, "dim_y" : n_y, "dim_z" : n_z }.
The variables `n_x`,`n_y`,`n_z` normally represent the number of webworker-threads in each dimension. The using of following method 
is pretty much similar to using `createPool` method above with only one difference that we must create a three dimensional array of
its arguments such as a tuple of two values of either a thread callback function or an array of each threads arguments. Notice that,
the callback function for each particular thread in a grid might be different. The same as `createPool` method, `createGrid3d` 
is the promise-based asynchronously executed method that invokes `then` method's callback function at the end of its execution.
The parameter variable `ret` is a three-dimensional array, in which each item is actually a return value of each particular thread
in a grid.

# Conclusion

That's All Folks :)

# Author

Arthur V. Ratz @ Epsilon Software Development Labs.