1.0.4 • Published 4 years ago

squid-workers v1.0.4

Weekly downloads
10
License
MIT
Repository
github
Last release
4 years ago

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'
]
1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago