This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

asyncparallel

0.0.18 • Public • Published

A small library to spaw a Function on multiple core.

INTRODUCTION

As you know, NodeJS is single thread then you can't use parallel approach over multiple core. This can be a problem when you should use Multiple Core. The solution in this library is to use Process.Fork() in some transparent way for the developer. Inside an external file (MyChild.js) you will define all Functions that you will call from the Main.js (your main file). The most useful use case is when you have an Array and you want to compute each element in Parallel. If MyChild fail for some reason, then you will receive the result from the Child that has successful answer (plus the Child that will sent the Error result) Don't worry the library is also able to wakeup again any Child if die (similar to Forever).

So if you have 4 Thread available, then you should tell the library to use it. If some Child will exit/die then for some seconds you will have only 3 thread available until the 4 came back to life.

Starting a Child is very fast, anyway depend from the 'startUp' script the child will have (for example connect to DB): this depend from your code.

I'm using this library for Machine Learning task, still not in production but is working fine.

MAIN.js (CONFIGURATION)

var childModuleToRun:string = "./TestSuite/MyChildSingleCore.js";

var env_param:Array<string> = ["mode=development"];

var numbersOfCore = 2;

var myAsync:AsyncParallel.AsyncParallel = new AsyncParallel.AsyncParallel (childModuleToRun, env_param, numbersOfCore);

	// i should wait for the childConnection
	myAsync.onConnected = function () {

		var childMethodToInvoke:string = "computeSum"; // the Child should receive a 'action' and answer!
		var paramsToSend:any = [0,1,2]; // each Child will receive different params (0, 1, 2) but can be any 'objects'

		myAsync.forEachParallel(childMethodToInvoke, paramsToSend, function (results:Array<any>) {

			console.log("Result Received1");

			for(var va:number=0;va<results.length;va++) {

				console.log("   Results JSON", results[va]);
			}
		});
	}

OUTPUT

Results null { args: 0, data_received: { action: 'computeSum', data_received: 10 } }

Results null { args: 1, data_received: { action: 'computeSum', data_received: 11 } }

Results null { args: 2, data_received: { action: 'computeSum', data_received: 12 } }

MyChildSingleCore.ts

if(process.send!=null) {
    var timeout:number = 5000+Math.random()*5000; // i want to simulate some delay...
    var index:number = parseInt(process.argv[2]); // the env_param received from the Parent

    process.on("message", function (message_received) {

        if(message_received.action=="computeSum") {

            setTimeout(function () {

                if(Math.random()<0.4) {

					// compute process and give back the Result to the Parent
                    process.send({action:"computeSum", data_received:message_received.args+10});

                } else {

						// if the process Crash, the Parent is able to catch the error and emit an Error event
						// the parent is also able to wake-up the Child again (for different data, not the same).
                        throw new Error("SimulatedCrash");
                }

            }, 1000*3);
        }
    });

	// i need to notice that I'm ready to receive data (otherwise the Parent will never send me something)
    process.send({action:"connected", index:index});
}

NOTE

This is a work in progress project since I'm working in scenario where i need MultipleCore for machine learning system.

Daniele Tassone

feedback/suggestions:

daniele.tassone2008@gmail.com

Package Sidebar

Install

npm i asyncparallel

Weekly Downloads

2

Version

0.0.18

License

ISC

Last publish

Collaborators

  • danieletassone