2.0.0 • Published 6 years ago

multithreading-toolkit v2.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Multithreading toolkit

Install:

npm install multithreading-toolkit --save

workerThreadsFunction - Execution of a function in a separate thread (based on worker_threads module). Available for Node.js version 10.5.0 and higher.

workerThreadsFunction args:

  • source: Function or path to the module that exports the function (Function/String)
  • options: Options when creating a function (Object):
  • options.returnTimeout: Return timeout (milliseconds) (Default: 60000)
  • options.eval: true, if the function was passed as the source, false if the path to the module was passed as the source (Default: false)
  • options.execArgv: execArgv for Worker instance (Array) (Default: [])
  • options.pool: Do I need to use a pool of workers (Bool) (Default: false)
  • options.poolOptions: Pool options for generic-pool

Example:

'use strict';

const myReturnTimeout = 1000000;
const { workerThreadsFunction } = require('multithreading-toolkit');

const someFunction = workerThreadsFunction(function (arg1, arg2, arg3) {
    // arg1, arg2, arg3 - Some data for calculations
    // Some heavy calculations, which usually block the thread
    return 'Some result';
}, {
    eval: true
});

// Call the function, passing in an array of arguments
someFunction([1, 2, 3], {returnTimeout: myReturnTimeout}).then(console.log).catch(console.error);

forkFunction - Execution of a function in a separate thread (based on child_process.fork).

forkFunction args:

  • source: Function or path to the module that exports the function (Function/String)
  • options: Options when creating a function (Object):
  • options.returnTimeout: Return timeout (milliseconds) (Default: 60000)
  • options.eval: true, if the function was passed as the source, false if the path to the module was passed as the source (Default: false)
  • options.forkOptions: options for child_process.fork
  • options.pool: Do I need to use a pool of workers (Bool) (Default: false)
  • options.poolOptions: Pool options for generic-pool

Example:

'use strict';

const myReturnTimeout = 1000000;
const { forkFunction } = require('multithreading-toolkit');

const someFunction = forkFunction(function (arg1, arg2, arg3) {
    // arg1, arg2, arg3 - Some data for calculations
    // Some heavy calculations, which usually block the thread
    return 'Some result';
}, {
    eval: true
});

// Call the function, passing in an array of arguments
someFunction([1, 2, 3], {returnTimeout: myReturnTimeout}).then(console.log).catch(console.error);
2.0.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago