1.0.4 • Published 3 years ago

node-threading v1.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Node-threading

The brand-new solution for easy threading in node.

Threading can sometimes be a time-consuming thing to do, and rather confusing thing aswell (Especially for beginners) That changes today, instead of messing around with isMainThread and other files we now allow you to thread a function itself! The threads are as simple, if not simplet than python threading. node-threading is an upcoming library to reïnvent how threading is done in nodejs. We are planning to see if we can sync variables across thread/parent in the near future. for now every thread is a seperate instance of node, like the included sollution in node. We currently do not have a git repo but when it is at a stable point we will create a git repo for this library. PLEASE NOTE! This libary is not ment for production products yet, you can use it in production products. But we strongly advise you not to. With that out of the way we hope you will enjoy the simplicity of node-threading

Discord: https://discord.gg/zMtJQafwhh

Examples:

let { Thread } = require("node-threading");

function foo() {
    console.log("Bar")    
}

new Thread(foo).start();
let { Thread } = require(`node-threading`);

function foo() {
    setInterval(() => {
        console.log("bar") //Send bar
    }, 90) //Enough delay to send bar 5x in 500ms
}

for (let i = 0; i < 2; i++) {
    let t = new Thread(foo);
    t.start(); //Start thread
    setTimeout(() => {
        t.terminate(); //Kill thread
    }, 500) //Allow 2 threads to send 5x Bar totaling in 10x bar
}
function foo() {
    let { Parent } = require(`./index.js`);
    let parent = new Parent();

    parent.on("test", (data) => {
        console.log(data); //logs "works"
    })
}

for (let i = 0; i < 10; i++) {
    let t = new Thread(foo);
    t.start();
    t.send("test", "works")
}

Class - Thread

Create constructor let t = new Thread(function, { cwd: dir, modules: dir}); (Options are optional)

cwd = Run directory
modules = node modules dir

Start the thread t.start();

stop the thread t.terminate();

send message to thread t.send(channel, data)

recieve message from thread t.on(channel, (data) => {})

Class - Parent

//this code must be in the thread
let { Parent } = require(`node-threading`); //Require parent from node-threading, make sure to import it from the thread as variables as of now are not shared
let parent = new Parent(); //Create new instance

get message from parent parent.on(channel, (data) => {})

send message to the parent parent.send(channel, data)

Changelog

1.0.4

* Update readme

1.0.3

+ Add parent class for IPC and getting data in the thread
+ Add IPC
+ Add thread.send and parent.send
+ Extend events to thread and parent classes

1.0.2

+ Fix require statement
+ Add thread.start() and thread.terminate()

1.0.1

+ Thread class
+ Fix Filesystem lib

1.0.0

+ Add Readme
1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago