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-multiprocess

v0.0.2

Published

Easy multiprocessing in nodejs

Downloads

11

Readme

Easy multiprocessing in nodejs

Version Downloads/week License

About

node-multiprocess makes multiprocessing in nodejs simple. Sometimes there are CPU bound tasks that are required in nodejs, node-multiprocess allows a node program to run concurrently to keep IO moving on the main thread.

Install

$ npm i node-multiprocess
$ # or
$ yarn add node-multiprocess

Usage

// node-multiprocess exports a singleton and a class to be used
// the default export is the singleton class
import SingletonPool from 'node-multiprocess';
// or
import { SingletonPool } from 'node-multiprocess';

// a regular class can be imported like this
import { Pool } from 'node-multiprocess'; 

Singleton usage

There will only ever be one instance of the singleton class

// Before getting first instance of singleton class, you can set the number of processes 
SingletonPool.setMaxProcesses(4); // default is the number of CPUs on machine
// The timeout can also be adjusted in seconds
SingletonPool.setTimeout(120); // default is 60 seconds

const pool = SingletonPool.getInstance();
...
const pool = SingletonPool.getInstance(); // same instance as above

Class usage

There can be many instances of the Pool class, however, it is not recomended to have more than one at a time because this can cause performance issues

// Number of process can be set either before instantiation or during
// before 
Pool.MAX_PROCESSES = 4; // default is the number of CPUs
// during
const pool = new Pool(4); // both create a pool with four processes
// The timeout can also be adjusted before instantiation
Pool.TIMEOUT = 120; // default is 60 seconds
const secondPool = new Pool();
// ^ Pool will have four processes and a timeout of 120 seconds
// create `secondPool` before killing `pool` will cause their to be four additional worker processes alive

Adding jobs

Jobs can be aded by calling the addJob method. This method returns a promise of the resulting type from the function that is passed to the method

const pool = new Pool(4);

function fibSlow(n: number): number {
    if (n <= 0) {
        return 1;
    }
    return fibSlow(n-1) + fibSlow(n-2);
}

const promises = []
for (let i = 1; i < 6; i++) {
    const resultPromise = pool.addJob(fibSlow, i);
    promise.push(resultPromise)
}

const promisesResults = await Promise.all(promises);
promisesResults.forEach(result => console.log(result));
// 1 2 3 5 8

The addJob method can accepts any number of arguments and types

function addThree(x: number, y: number, z: number): number {
    return x + Y + Z;
}
pool.addJob(addThree, 10, 15, 20);
// Promise<number> => 45

function mergeObj(obj1: Object, obj2: Object): Object {
    return {
        ...obj1,
        ...obj2
    };
}
pool.addJob(mergeObj, {a: 'a'}, {b: 'b'});
// Promise<Object> => {a: 'a', b: 'b'}

Killing process pool

When finished with process pool and all jobs are complete use the kill method to destroy all process

pool.kill(); // sends SIGINT to all child processes