# thread-pool-node

> Worker threads pool based on generic-pool

Latest version **1.0.9** (published 2021-05-12) · ISC license · 0 weekly downloads

## Install

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

## 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.0.9 |
| Published | 2021-05-12 |
| First published | 2019-04-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10.6.0 |
| Dependencies | 1 |
| Unpacked size | 3.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Idan Attias |
| Maintainers | idan-at, harelmoshe |
| Keywords | thread, worker, worker_threads, worker-threads, pool, threadpool, multithreading, threads, node-threads |

## Links

- npm: https://www.npmjs.com/package/thread-pool-node
- Homepage: https://github.com/idan-at/thread-pool-node
- npm.io page: https://npm.io/package/thread-pool-node

## Dependencies (1)

- [generic-pool](https://npm.io/package/generic-pool.md) ~3.7.0

## 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.0.9 (latest) — 2021-05-12
- 1.0.8 — 2020-02-05
- 1.0.7 — 2019-09-09
- 1.0.6 — 2019-08-26
- 1.0.5 — 2019-07-18
- 1.0.4 — 2019-05-05
- 1.0.3 — 2019-05-02
- 1.0.2 — 2019-04-14
- 1.0.1 — 2019-04-14
- 1.0.0 — 2019-04-11

## README

# Thread Pool

[![npm version](https://badge.fury.io/js/thread-pool-node.svg)](https://badge.fury.io/js/thread-pool-node)
[![Build Status](https://travis-ci.org/idan-at/thread-pool-node.svg?branch=master)](https://travis-ci.org/idan-at/thread-pool-node)

A Thread pool for nodejs [worker-threads](https://nodejs.org/api/worker_threads.html). It relies on the [generic-pool](https://www.npmjs.com/package/generic-pool) library to handle the resource management.

## Install
`npm install thread-pool-node`

or with yarn:

`yarn add thread-pool-node`

## Usage Example
```js
// index.js
const createPool = require('thread-pool-node')

const pool = createPool({
  workerPath: './path/to/worker.js',
  workerOptions: {
    workerData: {
      magicNumber: 42
    }
  },
  poolOptions: { // passed to generic-pool
    min: 2,
    max: 4
  }
})

const worker = await pool.acquire();
const onMessage = result => {
  // do something with the result
  console.log({ result });

  // release back to thread pool
  pool.release(worker);
  worker.removeListener("message", onMessage);
};

worker.on("message", onMessage);
worker.postMessage(args);
```

```js
// worker.js
const { parentPort, workerData } = require("worker_threads");

parentPort.on("message", message => {
  parentPort.postMessage(aCPUBoundTask(workerData.magicNumber))
});
```

## Pool Basic API
- `createPool`: Creates a new worker-threads pool according to the given [pool options](https://github.com/coopernurse/node-pool#createpool)
- `async Pool#acquire` - Returns a new ready to be used worker from the pool.
- `async Pool#release` - Releases the worker back to the pool.

For info on how to configure the pool to meet your needs, and more useful pool APIs, see [generic-pool](https://github.com/coopernurse/node-pool#readme)

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