# worker-forker

> Simplifies forking of worker processes and distributing tasks between them.

Latest version **0.0.2** (published 2013-08-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install worker-forker
pnpm add worker-forker
yarn add worker-forker
bun add worker-forker
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2013-08-29 |
| First published | 2013-08-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Daniil Bratchenko |
| Maintainers | daeq |
| Keywords | fork, worker, tasks, cluster, child_process |

## Links

- npm: https://www.npmjs.com/package/worker-forker
- Repository: https://github.com/daeq/node-worker-forker
- Issues: https://github.com/daeq/node-worker-forker/issues
- npm.io page: https://npm.io/package/worker-forker

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

- 0.0.2 (latest) — 2013-08-29
- 0.0.1 — 2013-08-22

## README

Worker Forker
==================

Simplifies spawning pool of worker processes and distributing tasks between them.

Installation
----------
`npm install worker-forker`


Usage
-------

In master process:
```javascript
Forker = require('worker-forker').Forker;

// Create forker. Specify worker file.
var forker = new Forker(__dirname + "/worker.js");

// Spawn some worker processes.
forker.spawn(require('os').cpus().length);

// Add some tasks
forker.addTask("task parameters", function(err, result1, result2) {
    console.log("Got task result:", err, result1, result2);
});

// Handle various errors
forker.on('error', function(err) {
    console.log("Got error:", err);
});

// Wait until all tasks finished
forker.finish(function() {
    console.log("All tasks finished!");
});
```

In worker file (worker.js in this example):
```javascript
var Worker = require('../index.js').Worker;

// Init worker with function which executes tasks.
var worker = new Worker(function(parameters, callback) {
    setTimeout(function() {
        console.log("Worker got task:", task parameters);
        callback(null, "result 1", "result 2");
    }, 1000);
});

// Start processing tasks.
worker.start();
```

**NB!** Task parameters and results are transformed to strings due to interprocess communication limitations. If you want to pass objects either way, you should handle conversion to/from strings yourself.

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