# worker-threads-pool

> Easily manage a pool of Node.js Worker Threads

Latest version **2.0.0** (published 2018-10-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install worker-threads-pool
pnpm add worker-threads-pool
yarn add worker-threads-pool
bun add worker-threads-pool
```

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2018-10-14 |
| First published | 2018-06-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/worker-threads-pool) |
| Module format | CommonJS |
| Node | >=10.5.0 |
| Dependencies | 1 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 427 |
| Author | Thomas Watson |
| Maintainers | watson |
| Keywords | pool, worker, workers, thread, threads, worker_threads, parallel |

## Links

- npm: https://www.npmjs.com/package/worker-threads-pool
- Repository: https://github.com/watson/worker-threads-pool
- Homepage: https://github.com/watson/worker-threads-pool#readme
- Issues: https://github.com/watson/worker-threads-pool/issues
- npm.io page: https://npm.io/package/worker-threads-pool

## Dependencies (1)

- [after-all](https://npm.io/package/after-all.md) ^2.0.2

## 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

- 2.0.0 (latest) — 2018-10-14
- 1.0.0 — 2018-06-24

## README

# worker-threads-pool

Easily manage a pool of [Node.js Worker
Threads](https://nodejs.org/api/worker_threads.html).

[![npm](https://nodei.co/npm/worker-threads-pool.png)](https://www.npmjs.com/package/worker-threads-pool)

[![Build status](https://travis-ci.org/watson/worker-threads-pool.svg?branch=master)](https://travis-ci.org/watson/worker-threads-pool)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)

## Installation

```
npm install worker-threads-pool --save
```

## Prerequisites

Worker Threads in Node.js are still an experimental feature and is only
supported in Node.js v10.5.0 and above. To use Worker Threads, you need
to run `node` with the `--experimental-worker` flag:

```
node --experimental-worker app.js
```

## Usage

```js
const Pool = require('worker-threads-pool')

const pool = new Pool({max: 5})

for (let i = 0; i < 100; i++) {
  pool.acquire('/my/worker.js', function (err, worker) {
    if (err) throw err
    console.log(`started worker ${i} (pool size: ${pool.size})`)
    worker.on('exit', function () {
      console.log(`worker ${i} exited (pool size: ${pool.size})`)
    })
  })
}
```

## API

### `pool = new Pool([options])`

`options` is an optional object/dictionary with the any of the following properties:

- `max` - Maximum number of workers allowed in the pool. Other workers
  will be queued and started once there's room in the pool (default:
  `1`)
- `maxWaiting` - Maximum number of workers waiting to be started when
  the pool is full. The callback to `pool.acquire` will be called with
  an error in case this limit is reached

### `pool.size`

Number of active workers in the pool.

### `pool.acquire(filename[, options], callback)`

The `filename` and `options` arguments are passed directly to [`new
Worker(filename,
options)`](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options).

The `callback` argument will be called with the an optional error object
and the worker once it's created.

### `pool.destroy([callback])`

Calls
[`worker.terminate()`](https://nodejs.org/api/worker_threads.html#worker_threads_worker_terminate_callback)
on all workers in the pool.

Will call the optional `callback` once all workers have terminated.

## License

MIT

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