npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-threadpool

v1.5.5

Published

Thread pools using node's new worker_thread package

Downloads

71

Readme

node-threadpool

CircleCI npm version codecov dependencies Status semantic-release

WARNING: This project is mostly experimental and the API is subject to change.

This package implements thread pools using node 10.5's new worker thread API (see: https://nodejs.org/api/worker_threads.html).

Features

  • Lightweight: one dependency (surrial) for serialization
  • Simple API: submit a function, await a result (no need to mess with loading from files, strings, etc.)
  • Supports transpiled code (ex: you may use Typescript to define your workers)
  • Can send most types of data including maps, sets, etc.
  • Supports shared data between threads, see the example

Why

Worker threads are usually expensive to create, a thread pool maintains the threads and allows you to submit work on the fly, without having to pay the cost of recreating threads.

With node's new worker_thread API, threads in the pool can pass messages to each other and read and write to shared memory.

Usage

Full API documentation can be found here: https://psastras.github.io/node-threadpool/api/modules/executors.html.

If you're familiar with Java's thread pool API, this should be very familiar:

import { Executors } from "node-threadpool";

const pool = Executors.newFixedThreadPool(1);
const result = pool.submit(async () => "hello world");

console.log(await result); // prints "hello world"

Requires node 10.5+. You must run node with the --experimental-worker flag enabled.

NODE_OPTIONS=--experimental-worker ./server.js

or

node --experimental-worker ./server.js

Detailed Usage Instructions

To install:

yarn add node-threadpool

or

npm install node-threadpool

Import node-threadpool:

import { Executors } from "node-threadpool";

Executors contains methods to create different thread pools.

Create a thread pool by calling one of these methods:

// creates a thread pool with 4 threads
const pool = Executors.newFixedThreadPool(4);

Then submit work to the pool with the submit method. This method takes in a function with no arguments that returns a Promise. The submit method itself returns a Promise which is resolved when the function has been executed.

// these execute in parallel (as long as the pool size >= 2)
const result1 = pool.submit(async () => "done 1");
const result2 = pool.submit(async () => "done 2");

console.log(await result1); // joins and prints "done1"
console.log(await result2); // joins and prints "done2"

See the documentation for full API details.

Note: if you're not using async / await, Promise based functions work just as well.

Warning

You may only access data within the runnable function's context. For example, this is an error:

const hello = "hello";
await pool.submit(async () => hello);

Instead, use the optional data object when submitting the function:

const hello = "hello";
await pool.submit(async (data) => data, hello);

Similarly you must require third party modules from inside the run method:

await pool.submit(async () => {
  const fs = require('fs');
  fs.readFileSync('README');
});

Examples

Basic Usage

const pool = Executors.newFixedThreadPool(4);
const result = pool.submit(async () => "hello world");
console.log(await result); // prints "hello world"

Pass Data

const pool = Executors.newSingleThreadedExecutor();
const map = new Map();
map.set("key", "value");
const data = {
  map
};
const result = pool.submit(async d => d.map.get("key"), data);
console.log(await result); // prints "value"

Shared Data

Simple shared array buffer

const buffer = new SharedArrayBuffer(1 * Int32Array.BYTES_PER_ELEMENT);
const array = new Int32Array(sharedBuffer);

// theres no lock, so in order to write safely we'll use one thread for this toy example
// see the next example for atomic usage
const pool = Executors.newSingleThreadedExecutor();

// set the data in the shared buffer to 42
await pool.submit(async d => (new Int32Array(d)[0] = 42), buffer);

// read the data from the shared buffer
const result = pool.submit(async d => new Int32Array(d)[0], buffer);

console.log(await result); // prints 42

Atomics

const buffer = new SharedArrayBuffer(1 * Int32Array.BYTES_PER_ELEMENT);
const array = new Int32Array(buffer);
const pool = Executors.newSingleThreadedExecutor();

const result = pool.submit(async d => {
  const view = new Int32Array(d);
  Atomics.wait(view, 0, 0); // wait here until the value is no longer 0
  return Atomics.load(view, 0);
}, buffer);

Atomics.store(array, 0, 1); // change the value from 0, unblocking the worker thread

console.log(await result); // prints 1

TODOs

  • Figure out better function / data serialization.
  • Support cached thread executor
  • Clean up code
  • Settle on an API (when node's api is stable)
  • Support nested shared buffer serialization

License

MIT Licensed, see the LICENSE file.