# node-workers-pool

> Easy way to manage a pool of worker threads.

Latest version **1.1.4** (published 2019-12-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-workers-pool
pnpm add node-workers-pool
yarn add node-workers-pool
bun add node-workers-pool
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2019-12-25 |
| First published | 2019-01-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Bruno Oliveira |
| Maintainers | bruno303 |
| Keywords | thread, pool, worker_threads, workers, threads, worker, async, cpu-bound, parallelism, parallel, process, worker_thread, queue, task, threadpool |

## Links

- npm: https://www.npmjs.com/package/node-workers-pool
- Repository: https://github.com/bruno303/node-workers-pool
- Homepage: https://github.com/bruno303/node-workers-pool#readme
- Issues: https://github.com/bruno303/node-workers-pool/issues
- npm.io page: https://npm.io/package/node-workers-pool

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 1.1.4 (latest) — 2019-12-25
- 1.1.3 — 2019-01-27
- 1.1.2 — 2019-01-15
- 1.1.1 — 2019-01-09
- 1.1.0 — 2019-01-09
- 1.0.1 — 2019-01-07
- 1.0.0 — 2019-01-07

## README

# node-workers-pool
Easy way to manage a pool of worker threads.

In Nodejs v12.14 **LTS** or later, the **--experimental-worker** flag is not necessary anymore, since this resource become stable.

In Nodejs up to 11.x, use the **--experimental-worker** flag to run correctly, since this resource still experimental.

## Introduction

With this package you can:
* Run heavy cpu-bound in a pool of worker_threads, an experimental resouce in NodeJs.
* Control the number of active workers in the pool.
* Create a queue, because when all the workers are busy, new processing requests will be queued.
* Easily clear the pool (workers and queue).
* Easily capture the result or error from the workers, since the pool uses Promise.

### Prerequisites

* [NodeJs](https://nodejs.org/en/) (v 12.x or later for stable / v 10.15.0 up to v 11.x for experimental)
* [Npm](https://www.npmjs.com/)

### DEV - Prerequisites
* [express](https://expressjs.com/)
* [nodemon](https://nodemon.io/)
* [eslint](https://eslint.org/)

### Examples

* Creating a pool with max 10 workers.
```
const pool1 = require('node-workers-pool')({
    max: 10,
    queueMax: 40
});

--or

const Pool = require('node-workers-pool');
const pool2 = Pool({ max: 10, queueMax: 40 });
```

* If options is missing, the max number of workers will be the core's number of the machine (require('os').cpus().length).

* Creating a task and sending to queue
```
const now = new Date();
const pool = require('node-workers-pool')({ max: 4, queueMax: 10 });

              /* Function to execute*/          /* Params */
pool.enqueue((num1, num2) => num1 + num2,       15, 30)
.then(result => console.log(`Executed in ${(new Date() - now) / 1000} sec(s). Result: ${result}`))
.catch(err => console.err(err));
```

* Another example
```
const now = new Date();
const pool = require('node-workers-pool')({ max: 4, queueMax: 10 });

function fibonacci(num) {
    if (num < 2){
        return num
    }
    return fibonacci(num - 1) + fibonacci(num - 2);
}

pool.enqueue(fibonnaci, 40) // Function and parameter
.then(result => console.log(`Executed in ${(new Date() - now) / 1000} sec(s). Result: ${result}`))
.catch(err => console.err(err));
```

* Multiple parameters example
```
const pool = require('node-workers-pool')({ max: 4, queueMax: 10 });

function mult(num1, num2, num3, num4) {
    return num1 * num2 * num3 * num4;
}

pool.enqueue(mult, 3, 5, 8, 10) // Function and parameters
.then(result => {
    console.log(`Executed in ${(new Date() - now) / 1000} sec(s). Result: ${result}`);
    pool.finishPool()
    .then(() => console.log('Finished!'));
})
.catch(err => console.err(err));
```

* Cleaning the pool
```
const pool = require('node-workers-pool')({ max: 4, queueMax: 10 });

pool.enqueue(() => 'Executed') // Function and parameters
.then(result => {
    console.log(result);
    pool.finishPool()
    .then(() => console.log('Pool finished!'));
});
```

* Default values
```
const Pool = require('node-workers-pool');
const pool = new Pool(); // without opts

/** This pool will be configured by default with:
*   max = require('os').cpus().length
*   queueMax = 10
*/
```

### Tests

*Obs: Express processes identical requests one after another. Performing the same get from the same host with the same parameters can make it not run asynchronously, due to the express.*

Run the code below to test:
```
npm test
```
This will start an express server with 3 async routes and 3 sync routes.
You can test all of them in your browser and see that sync routes will lock all the others, while async routes will allow other request, because they don't lock the Event Loop.

Examples:
* Event loop free (async):
```
// Tab 1 - run:
http://127.0.0.1:3000/asyncsum/10000000000

// Tab 2 - run:
http://127.0.0.1:3000/asyncsum/10
// the second request will answer even while the first still running.
```

* Event loop blocked (sync):
```
// Tab 1 - run:
http://127.0.0.1:3000/syncfibo/44

// Tab 2 - run:
http://127.0.0.1:3000/asyncfibo/5
// the second request will wait the first finish, because the fist is running in the event loop.
```

### Notes
* Case the pool and queue are full, an error 'full' will be thrown.
* The workers will be allocated as needed, so just create a pool will not create all workers at same time.

Click [here](https://nodejs.org/docs/latest-v12.x/api/worker_threads.html) to read more about **worker_threads**.

## Author

* **Bruno Oliveira** - [Github](https://github.com/bruno303)

## License

The project is licensed under the MIT License. See the [LICENSE](https://github.com/bruno303/node-workers-pool/blob/master/LICENSE) file for more details

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