Skip to content

About

A convenient worker thread pool implementation for Node.js

Resources

Stars

12 stars

Watchers

1 watching

Forks

Latest commit

 

History

49 Commits

Folders and files

Repository files navigation

wise-workers test

A worker thread pool for CPU-bound tasks. It requires no configuration and has many powerful features:

  • Generator and AsyncGenerator function support
    • Worker functions can be generator functions or async generator functions, making it easy to stream results back to the main thread. Iteration happens eagerly, to maximize parallelism (i.e., the main thread does not pause the generator function).
  • Functions (callbacks) as arguments
    • Functions can be passed to worker tasks. They become async functions in the worker thread, using MessagePort for communication under the hood.
  • Cancellation support
  • Automatic thread management
    • Crashed threads are automatically respawned, unless they're crashing during startup (to prevent an infinite spawn loop).
  • Zero-copy data transfer
    • Data can be efficiently moved between threads without copying it. This is beneficial to performance when passing large Buffers between threads.

Installation

npm install wise-workers

Requires Node.js v16.x.x or later.

Usage

const ThreadPool = require('wise-workers');

const pool = new ThreadPool({ filename: require.resolve('./worker') });

const result = await pool.call('add',