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) {
return 'Some result';
}, {
eval: true
});
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) {
return 'Some result';
}, {
eval: true
});
someFunction([1, 2, 3], {returnTimeout: myReturnTimeout}).then(console.log).catch(console.error);