# asyncparallel

> Small library (Like Async.JS) but is able to spaw a function over multiplecore

Latest version **0.0.18** (published 2015-10-29) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install asyncparallel
pnpm add asyncparallel
yarn add asyncparallel
bun add asyncparallel
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.18 |
| Published | 2015-10-29 |
| First published | 2015-10-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Daniele Tassone |
| Maintainers | danieletassone |
| Keywords | parallel, asyncjs, parallel computing, process, process fork |

## Links

- npm: https://www.npmjs.com/package/asyncparallel
- npm.io page: https://npm.io/package/asyncparallel

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 0.0.18 (latest) — 2015-10-29
- 0.0.17 — 2015-10-29
- 0.0.16 — 2015-10-29
- 0.0.15 — 2015-10-29
- 0.0.14 — 2015-10-29
- 0.0.13 — 2015-10-29
- 0.0.12 — 2015-10-28
- 0.0.11 — 2015-10-28
- 0.0.10 — 2015-10-28
- 0.0.9 — 2015-10-28
- 0.0.8 — 2015-10-28
- 0.0.7 — 2015-10-28
- 0.0.6 — 2015-10-28
- 0.0.5 — 2015-10-28
- 0.0.4 — 2015-10-28
- … 3 more at https://npm.io/package/asyncparallel/versions

## README

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

---
_Source: https://npm.io/package/asyncparallel · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
