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

worker-pool-task-queue

v1.0.6

Published

The library is specifically designed for Node.js environments and is based upon the native worker_threads module

Downloads

105

Readme

worker-pool-task-queue

This library provides a solution for managing concurrent tasks in a Node.js environment. It leverages a combination of worker threads organized into a pool and a task queue system to efficiently execute tasks, ensuring optimal utilization of system resources and enhancing application performance.

Table of Contents

About

The library is specifically designed for Node.js environments and is based upon the native worker_threads module (https://nodejs.org/api/worker_threads.html).

  • Worker Pool Management:

The library creates a pool of worker threads with a specified size. Workers are managed efficiently, ensuring they are available for processing tasks when needed.

  • Task Queue System:

Implements a task queue mechanism to handle tasks that cannot be immediately processed due to the unavailability of worker threads. Queued tasks are executed in a sequential manner, preventing overload and ensuring reliable task execution.

  • Dynamic Worker Pool Scaling:

Dynamically scales the worker pool size based on demand, ensuring optimal resource utilization. New workers are added to the pool as needed, allowing the system to handle increased workloads effectively.

Installation and usage

  • Install the Worker Pool Module

First, install the worker-pool-task-queue module using npm:

$ npm install worker-pool-task-queue
  • Create a Worker Pool

Import the WorkerPool class from the installed module and set up a worker pool with a specific pool size, worker script file path, and maximum number of workers:

const WorkerPool = require('worker-pool-task-queue');
const path = require('path');

// Create a worker pool with 5 workers, using 'Worker.js' as the worker script
const pool = new WorkerPool(poolSize = 5, path.join(__dirname, 'Worker.js'), maxWorkers = 15);
  • Create the Worker.js File in ypur repro containing your functions. Note that functions can be handled in external libraries and classes (fn is representative for your functions, used with a delay in the example)
const { parentPort } = require('worker_threads');

parentPort.on('message', async (message) => {
    const { fn } = message;
    // execute 'myfunc' here, return result in postMessage
    setTimeout(() => {
        parentPort.postMessage({ executed: `function ${fn} true` });
    }, 1000);
});
  • Define and Execute Tasks

Create functions to execute tasks using the worker pool. For example, you can define an executeTask function that runs a specific function called 'myfunc' in the worker pool:

async function executeTask() {
    try {
        const result = await pool.runTask({ fn: 'myfunc', params: { set: true } });
        console.log(result);
    } catch (error) {
        console.error(error);
    }
}

You can then call this function multiple times using a loop or any other logic as needed:

async function executeTasksMultipleTimes() {
    for (let i = 0; i < 4; i++) {
        await executeTask();
    }
}

executeTasksMultipleTimes();

Support

This library used jsdoc types and is tested in Chrome

Changelog

  • 2024-11-07 - increaseWorkerPool naming Convention (v1.0.6)