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

@lhozdroid/threadpool-jsthreadpool-js

v1.0.0

Published

Lightweight and efficient JavaScript library for managing parallel tasks using Web Workers.

Downloads

10

Readme

ThreadPool

ThreadPool is a lightweight and efficient JavaScript library for managing parallel tasks using Web Workers. It is designed to optimize concurrency, providing support for task retries, timeouts, graceful shutdowns, and dynamic pool resizing.

Features

  • Dynamic thread pool size: Configure the number of concurrent workers.
  • Retries: Automatically retry failed tasks.
  • Timeouts: Set timeouts to prevent long-running tasks.
  • Auto-shutdown: Optionally shutdown the pool when all tasks are complete.
  • Graceful shutdown: Stop accepting new tasks and allow existing tasks to complete.
  • Task cancellation: Cancel queued tasks that haven't started yet.

Installation

You can directly import this library into your JavaScript project:

import ThreadPool from './ThreadPool';

Alternatively, you can package it as an NPM module and publish it for easy installation:

npm install threadpool-js

Usage

1. Create a ThreadPool

// Create a thread pool with 4 workers, 5-second task timeout, 2 retries, and auto-shutdown enabled.
const pool = new ThreadPool(4, 5000, 2, true);

2. Add Tasks to the Pool

You can use the execute() method to add tasks. It returns a Promise that resolves or rejects based on the task outcome.

const myTask = (data) => {
  return data.num * 2;
};

const data = { num: 10 };

pool.execute(myTask, data)
  .then(result => console.log("Task Result:", result))  // Outputs: Task Result: 20
  .catch(error => console.error("Task Error:", error));

3. Task Cancellation

If you want to cancel a task that is still queued, you can use the cancelTask() method.

pool.cancelTask(myTask);

4. Graceful Shutdown

To stop accepting new tasks but wait for all active tasks to finish:

pool.shutdown();

5. Killing the Pool

If you need to immediately stop the pool and discard all pending tasks:

pool.kill();

6. Resizing the Pool

You can dynamically resize the thread pool:

pool.resizePool(8);  // Change the pool size to 8 workers.

Use Cases

1. Batch Processing of API Calls

If you're sending multiple API requests and want to manage them concurrently, this library is perfect. Here's an example of sending 10 parallel API calls with a maximum of 3 workers:

const fetchData = async (url) => {
  const response = await fetch(url);
  return await response.json();
};

const urls = ["https://api.example.com/data1", "https://api.example.com/data2", ...];

urls.forEach(url => {
  pool.execute(fetchData, { url })
    .then(data => console.log("Fetched data:", data))
    .catch(error => console.error("Failed to fetch:", error));
});

2. Image Processing or File Handling

When working with large images or files, you can use the ThreadPool to offload processing tasks to multiple workers:

const processImage = (imageData) => {
  // Heavy computation, like resizing or filtering
  return performComplexImageProcessing(imageData);
};

const images = [...]; // Array of image data

images.forEach(image => {
  pool.execute(processImage, { image })
    .then(result => console.log("Image processed"))
    .catch(error => console.error("Image processing failed:", error));
});

3. Data Transformation (ETL)

Suppose you're processing a large dataset in chunks. You can distribute the workload across multiple workers to speed up ETL processes.

const transformData = (chunk) => {
  // Apply transformation to data chunk
  return performDataTransformation(chunk);
};

const dataset = [...];  // Large dataset split into chunks

dataset.forEach(chunk => {
  pool.execute(transformData, { chunk })
    .then(result => console.log("Chunk processed"))
    .catch(error => console.error("Processing failed:", error));
});

Contributions

We welcome contributions! If you have ideas for features or improvements, please submit a pull request or create an issue.


License

This project is licensed under the MIT License.