multicpu

1.0.4 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i multicpu

Weekly Downloads

0

Version

1.0.4

License

ISC

Unpacked Size

7.83 kB

Total Files

6

Last publish

Collaborators

  • devbab