Skip to content

KolAbuk/threads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@kolabuk/threads

Node.js threads

⚠️⚠️⚠️ Warning! Possible memory leak ⚠️⚠️⚠️

Installation

npm i @kolabuk/threads

Importing

import { Threads, threadFunc } from "@kolabuk/threads";

Usage

const func: threadFunc = async ({ workerID, startedCounter }, someMass) => {
  try {
    await new Promise((res) =>
      setTimeout(res, Math.floor(Math.random() * 15) * 1000)
    );
    return {
      workerID,
      success: Math.round(Math.random()) == 1,
      logText: `${startedCounter} ${someMass[startedCounter]}`,
    };
  } catch (e) {
    throw e;
  }
};
const threads = new Threads(
  func, //function with your logic
  (args) => console.log(args), //function for logs
  2, //threads count, >=1
  10 * 1000 //timeout time in ms
);
threads.run(
  15, //times to run threads
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] //any data you need
);
const stat = () => {
  //simple example of success/error statistic
  try {
    console.log(
      threads.getTrueCounter(),
      "/",
      threads.getFalseCounter(),
      "//",
      threads.getAllCounter()
    );
  } catch (e) {
    throw e;
  }
};
setInterval(stat, 10 * 1000);

About

Node.js threads

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published