# @banez/workers

> Package which will execute async function with workers.

Latest version **1.1.1** (published 2022-01-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @banez/workers
pnpm add @banez/workers
yarn add @banez/workers
bun add @banez/workers
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2022-01-20 |
| First published | 2021-11-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Branislav Vajagić |
| Maintainers | banez |

## Links

- npm: https://www.npmjs.com/package/@banez/workers
- Repository: https://github.com/Bbanez/workers
- Homepage: https://github.com/Bbanez/workers#readme
- Issues: https://github.com/Bbanez/workers/issues
- npm.io page: https://npm.io/package/@banez/workers

## Dependencies (1)

- [@banez/queue](https://npm.io/package/@banez/queue.md) ^1.0.0

## Recent versions

- 1.1.1 (latest) — 2022-01-20
- 1.1.0 — 2022-01-19
- 1.0.1 — 2021-11-19
- 1.0.0 — 2021-11-19

## README

# Workers

Workers in the context of this package are handler with will run asynchronous functions. In general, if there as N tasks and M workers, worker manager will assign 1 task to each available worker. When worker completes a task it will report back to manager. If there are uncompleted tasks in the pool, manager will assign a new task to the worker.

When assigning task to the manager, it will be added to the task pool. When a worker is free (not doing anything), manager will assign the task to it.

Idea is to use workers in conjunction with child process to utilize parallel processing, but worker task can be any asynchronous function.

## How to use

```ts
import { createWorkerManager } from '@banez/workers';

async function main() {
  // This will create a manager with 2 workers.
  const manager = createWorkerManager({ count: 2 });

  manager.assign(async () => {
    return await new Promise(() => {
      setTimeout(() => {
        console.log('Done after 200ms');
      }, 200);
    });
  });
  console.log(
    await manager.assign<string>(async () => {
      return 'Done instantly';
    }),
  );
}
main().catch((err) => {
  console.error(err);
  process.exit(1);
});
```

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