multithreading-toolkit

2.0.0 • Public • Published

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);
 

Package Sidebar

Install

npm i multithreading-toolkit

Weekly Downloads

0

Version

2.0.0

License

MIT

Unpacked Size

15.1 kB

Total Files

13

Last publish

Collaborators

  • inviz-custos