squid-workers
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

squid-workers

Multi-processing for NodeJS
Fork worker Nodes and send a module to run

// task.js

const runTask = (name) => {
    return `Hello ${name}`;
};

export default () => {
    return {
        msg: 'Hello World'
    };
}

export {
    runTask
}

// main.js
import WorkersHub from 'squid-workers';

const wh = new WorkersHub(6); // pass worker nodes count to fork

wh.send(`${__dirname}/task`)
    .then(result => {
        console.log(result.msg); // will print 'Hello World'
    });

wh.send(`${__dirname}/task`, 'runTask', 'squid')
    .then(result => {
        console.log(result); // will print 'Hello squid'
    });

wh.close(); // call close anytime, it will wait till all the workers are finished

Run array of tasks

import WorkersHub from 'squid-workers';

const wh = new WorkersHub(6);
Promise.all([
            'squid',
            'lobster',
            'crab',
            'scallop'
        ].map(item => wh.send(`${__dirname}/task`, 'runTask', item)))
    .then(results => {
        console.log(results);
    });
wh.close();
// output
[
  'Hello squid',
  'Hello lobster',
  'Hello crab',
  'Hello scallop'
]

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.4
    2
    • latest

Version History

Package Sidebar

Install

npm i squid-workers

Weekly Downloads

2

Version

1.0.4

License

MIT

Unpacked Size

10.2 kB

Total Files

4

Last publish

Collaborators

  • rajeevn