thread-process

    0.2.1 • Public • Published

    Thread Process Build Status

    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 // Gets the maximum CPU count
    Thread.OPEN_THREADS // Gets the count of how many threads are active

    Thread Class

    Constructor

    new Thread(); // Constructing a thread with default settings
    new Thread(function)// Stores a function

    Storing Functions (Asynchronous)

    (Async) returns the function stored

    thr.store(function);
    thr.store(function, function.name); // Override the function name or provide one that is missing

    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(); // Closes the thread after running

    Run target function stored in the thread

    thr.run("temp"); // Runs temp()

    Run with arguments

    thr.run(function.name, [arg1,arg2,arg3]) // temp(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)=>{}); // Contains the returned data
    thr.on("returned", (data)=>{}); // Contains data returned by RETURN(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); // Outputs "temp"
    }
    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();

    Install

    npm i thread-process

    DownloadsWeekly Downloads

    16

    Version

    0.2.1

    License

    MIT

    Unpacked Size

    120 kB

    Total Files

    10

    Last publish

    Collaborators

    • potatoparser