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

multicpu

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multicpu

distributes tasks on all available CPUs. Based on Cluster module

latest
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

MULTICPU

Distributes the processing of a list of items over all available CPUs.

Installation

1/ install the package:

npm install --save-dep multicpu

2/ Have a look at the example app.js

const multicpu = require("multicpu");

/**
 * build and return a list of items, to be sent to each worker
 * @param {int} cpu  number of CPU to be used
 * @returns {array} array of items, which will be sent one by one to the workers
 */
function start(cpu) {
    console.log("#CPU ", cpu);

    // build an array of N entries.
    let arr = [];
    for (let i = 2; i < 20; i++)
        arr.push({
            wait:i,
            detail:`will wait for ${i} seconds`
        });
    return arr;
}

/**
 * process an item from the list built by start, spread over workers running on all CPUs. n this example, wait for the numer of seconds provided in parameters
 * @async
 * @param {object} item one item of the list provided by start 
 */
async function processing(item) {
    console.log(`${process.pid} processing`, item);

    let p = new Promise((resolve) => {
        setTimeout(() => {
            console.log(`has waited ${item.wait} seconds`);
            resolve();
        }, item.wait * 1000);

    });
    await p;
    return item.wait
}


/**
 * called at the end of whole processing for master to do whatever work
 * @param {Object} list - list of {req,res,pid}
 * @param {Object} list.req - item from request
 * @param {Object} list.res - result from processing
 * @param {Object} list.pid - pid which did the processing
 */
function end(list) {
    console.log("end is called with this list",list);

    var dt = new Date();
    console.log(dt.toString());
}



multicpu.start({
    start: start,
    process: processing,
    end: end
});

© 2018-2019 devbab

FAQs

Package last updated on 06 Jan 2020

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