@kolabuk/threads
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@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);

Readme

Keywords

Package Sidebar

Install

npm i @kolabuk/threads

Weekly Downloads

2

Version

1.0.2

License

ISC

Unpacked Size

23.2 kB

Total Files

9

Last publish

Collaborators

  • kolabuk