Easy to use threading of functions in Node.js
simple-threads
Node.js module. Easy to use threading of functions in Node.js.
Usage
npm install simple-threads
Important to note: The method run is available on all functions and methods (also async ones) when you are using simple-threads. It runs the function in a separate thread and returns a promise that will be resolved when the function has finished.
someFunction;
Basic example
simpleThreads = ; { let randomNumberArray = ; // a loop that is time consuming whilerandomNumberArraylength < len randomNumberArray; return randomNumberArray;} // Run the function createRandomNumArr in a separate threadcreateRandomNumArr;
Options
Max child processes
Simple-threads creates a number of child processes to run threads in. You can specify this number manually. (If you don't the number of processors detected on your machine will be used.)
simpleThreadsmaxChildren = 4;
Simple threads will assign functions/"threads" to different child processes using a round-robin strategy.
Loading modules
A problem when using threads is that the modules loaded in the parent context are not automatically loaded in the threads/child processes. Simple threads solves this by providing a require method:
The require method of simple-threads will let us require modules so that they are available in all threads as global variables.
Use the form variableName:moduleName for each module. Or: If you don't provide a variable the name will be derived using snake-to-camelcase conversion: big-integer -> bigInteger etc.
simpleThreads;
Getting statistics
To get some statistics about cpu usage etc from simple-threads just ask for stats:
console;
Example of statistics
Advanced example
Please Note: Here we use simple-threads in conjunction with the npm module fair-share, which is a module that lets several cpu-intense function share cpu time in one single thread by interrupting them.
The example shows that simple-threads - can run several functions/"threads" in parallel in one child-process if they don't hog the cpu but rather perform asynchronous tasks.
// Load modulesconst simpleThreads = ; // The require method of simpleThreads will let us require modules// so that they are available in all threads as global variables.//// Use the form variableName:moduleName for each module.// Or: If you don't provide a variable the name will be derived// using snake-to-camelcase conversion: big-integer -> bigInteger etc. simpleThreads; // Max number of child processes - defaults to auto (which is the// number of cpus detected by Node.js). You don't have to explicitly// set auto - we just do it for clarity here. simpleThreadsmaxChildren = 'auto'; // Calculate Fibonacci numbers threaded and with interrupts static async { // Run the method algo as a thread by using the run method // that simple-threads automatically adds to all functions return await thisalgo; } // Calculate a fibonacci number static async { // We are using the npm module fair-share to run // several functions in parallel in one process/thread let req = n share = a = b = ; while n-- let temp = a; a = a; b = temp; await share; // fairShare interrupt return request: req stats: sharestats result: b ; } // Calculate 50 Fibonacci numbers between 4000 and 40000 { Object; this; } { forlet i = 1; i <= thistodo; i++ Fibonacci; } { console; thisdone++; thisdone == thistodo && this; } { console; // Some statistics about cpu-usage etc from simpleThreads console; } ;