npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

thread-process

v0.2.1

Published

Threading with JS through NodeJS Cluster

Downloads

134

Readme

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