New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

node-threadpool

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-threadpool

Thread pool constructs using node 10.5's new worker-thread package

Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
12
-55.56%
Maintainers
1
Weekly downloads
 
Created
Source

node-threadpool

CircleCI npm version semantic-release

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

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

Usage

https://psastras.github.io/node-threadpool/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"
import { Executors } from "node-threadpool";

const pool = Executors.newFixedThreadPool(1);
const result = pool.submit(async (): Promise<string> => "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.

TODOs

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

License

MIT Licensed, see the LICENSE file.

FAQs

Package last updated on 26 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts