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

tasks-pool

v1.0.8

Published

Tiny but light-weight and high-performed library to launch tasks asynchroniously in multiple processes and threads.

Downloads

10

Readme

tasks-pool - tiny but light-weight and high-performed library to launch tasks asynchroniously in multiple processes and threads.

Quick Start

install:

npm i tasks-pool

use:

const { Pool } = require("tasks-pool");

const generator = function* () {
    const items = [{ args: [1] }, { args: [2] }, { args: [3] }]; // list of arguments for handler
    for (const item of items) {
        yield item;
    }
}

const handler = val => {
    if (val === 3) throw Error(`Bad value ${val}!`)
    return new Promise(resolve => {
        setTimeout(() => resolve(val), 2000);
    });
}

const main = async () => {
    const pool = new Pool(handler, generator);
    pool.on('success', console.log);
    pool.on('error', console.log);
    await pool.run(); // wait for tasks scheduling
    await pool.wait(); // wait for tasks finishing
}

main();

Options

  • Generator can be async also:
const generator = async function* () {
    const items = [{ args: [1] }, { args: [2] }, { args: [3] }];
    for (const item of items) {
        yield await item;
    }
}
  • Tasks can have different weight (default is 1), which used for balancing:
const generator = async function* () {
    const items = [{ args: [1], weight: 1 }, { args: [2], weight: 2 }, { args: [3], weight: 3 }];
    for (const item of items) {
        yield await item;
    }
}
  • Tasks can have different retries. If not defined then pool retries is used:
const generator = async function* () {
    const items = [{ args: [1], retries: 1 }, { args: [2], retries: 2 }, { args: [3], retries: 3 }];
    for (const item of items) {
        yield await item;
    }
}
  • Pool can receive generator object as well as generator function:
new Pool(handler, generator)
new Pool(handler, generator())
  • Pool can receive even sequence as second argument:
new Pool(handler, [{ args: [1] }, { args: [2] }, { args: [3] }])
  • Pool second argument (if it's not like in above example) should follow iterator protocol - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators

  • Pool has third argument - named options: { workers, threads, retries, ha }:

    • workers - number of worker processes. By default is cpus number. If 0 no workers are forked and tasks are executed in master process.
    • threads - number of threads on worker (threads are organised via JavaScript async/await, native Node.js threads aren't used). By default is cpus number. Should be 1 minimum.
    • retries - number of retries for task if it's failed. By default is 0. Can be overwritten by task retries.
    • ha - Pass true if want to provide high-availability and to restart worker if it's finished suddenly. By default is false.
  • Pool raises events on task success or error.

pool.on('success', task => {
    console.log('task arguments', task.args);
    console.log('task result', task.result);
    console.log('task weight', task.weight);
    console.log('task max retries', task.retries);
    console.log('task was retried', task.retried);
});
pool.on('error', task => {
    console.log('task arguments', task.args);
    console.log('task error stack trace', task.error);
    console.log('task weight', task.weight);
    console.log('task max retries', task.retries);
    console.log('task was retried', task.retried);
});
  • Pool can be used as builder pattern:
new Pool()
    .handler((a, b) => {
        console.log(a, b);
    })
    .iterator(function* () {
        const iter = [{ args: [1, 3] }, { args: [2, 4] }];
        for (const it of iter) {
            yield it;
        }
    })
    .options({ workers: 2, threads: 2, retries: 2 })
    .run();