
Simple Threading with JS through NodeJS Cluster
const thread = require("thread-process");
function temp() {
console.log("Hello World ~Thread");
}
const thr = new Thread();
thr.store(temp);
thr.run();
thr.close();
Table of Contents
Features
- Supports "require" in "threaded" functions
- Supports storing both synchronous and asynchronous functions
- Multiple threads running simultaneously
- Thread emitted events
- Thread cleanup
Installing
Requires prior installation of NodeJS
$ npm install thread-process
Initializing
const thread = require("thread-process");
Properties
Thread.MAX_THREADS
Thread.OPEN_THREADS
Thread Class
Constructor
new Thread();
new Thread(function);
Storing Functions (Asynchronous)
(Async) returns the function stored
thr.store(function);
thr.store(function, function.name);
Global Variables Accessible
THREAD_DATA // (Object) Contains all the functions stored
FOCUSED_FUNCTION // (String) The most recent function
RETURN(data); // Sends data from the function to the main thread
Running Functions (Asynchronous)
Run the most recent function stored or executed by the thread
(Async) returns the data returned by the function executed
thr.run();
thr.runOnce();
Run target function stored in the thread
thr.run("temp");
Run with arguments
thr.run(function.name, [arg1,arg2,arg3])
thr.run([arg1,arg2,arg3])
Running Multiple Threads (Asynchronous)
(Async) returns data as an Array
Thread.runAll(thr.run(), ...);
Events
Event handling can make life much easier!
thr.on("completed", (data)=>{});
thr.on("returned", (data)=>{});
Closing Threads
thr.close();
Immediate Thread (Asynchronous)
Runs a function within a thread and immediately closes the thread upon completion
(Async) returns the data returned by the function executed
Thread.exec(function);
Thread.exec(function, [arg1,arg2,arg3]);
Example Usage
Using thread constiables
const temp = (text)=>{
console.log(THREAD_DATA);
console.log(FOCUSED_FUNCTION);
}
async function main(){
const tp = new Thread();
await tp.store(temp);
await tp.runOnce();
}
main();
Asynchronous management of threads & passing values
const temp = async (text)=>{
console.log(`Async! ${text}`);
}
async function main(){
const tp = new Thread();
await tp.store(temp);
await tp.runOnce(["~ Thread"]);
}
main();
Multiple stored functions
const func1 = ()=>{console.log("First function!")}
const func2 = ()=>{console.log("Second function!")}
async function main(){
const tp = new Thread();
await tp.store(func1);
await tp.store(func2);
await tp.runOnce("func1");
}
main();
Using then instead of async/await
const temp = ()=>{console.log("Hello World!")}
const tp = new Thread();
tp.store(temp).then((result)=>{tp.runOnce();});
Running simultaneous threads
const temp = ()=>{return "Hello";}
const tp = new Thread(temp);
const tp2 = new Thread(temp);
Thread.runAll(tp.runOnce(), tp.runOnce()).then((result)=>console.log(result));
Running with events
const temp = ()=>{RETURN("Hello")};
const tp = new Thread(temp);
tp.on("returned", (data)=>{
console.log(data);
tp.close();
});
tp.run();