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

workerrpc

v0.0.3

Published

Worker/Pool Abstraction for RedisRPC

Downloads

2

Readme

WorkerRPC.js

WorkerRPC is a utility module for node.js which allows to distribute javascript workloads easily across multiple instances (e.g. to make use of multi-core cpus).

Quick Examples

Basic Worker used for examples:

//worker.js START

//pi(num,callback) will create NUM 2D-points in [0;1[
//and count how many have distance of 1 or less to origin.
//Returns that count via callback

function pi(num,callback){
	var insideCircle = 0;
	for(var i=0;i<num;i++) {
		var p = {x:Math.random(),y:Math.random()};
		if (p.x*p.x+p.y*p.y <= 1) {
			insideCircle++;
		}
	}
	console.log("Sample Pi Run finished (" + num + " trials)");
	callback(insideCircle);
}

//add this to worker.js to expose function add
require("workerrpc").wrap({
  pi:pi
});

Variant #1: Combining results via simple callbacks/RPC

//main.js START
var numOfTrials = 1000*1000;
var results = {insideCircle:0,total:0};

var workloads = [];
for(var i = 0;i < 10; i++) {
  workloads.push({
    //filename that will be forked in a child-process with wrapped object
		script: "worker.js",
		action: function(instance,params,finished) {

  		//actual remote function to be called ("pi")
			instance.pi(numOfTrials,function(insideCircle) {
				results.insideCircle+=insideCircle;
				results.total+=numOfTrials;
				//call finished to make shure the worker is released from pool
				finished();
			});
		},

		//optional set of params to give worker
		params: {}
	});
}

//combine results once there are no more workers pending
var combineResults = function() {
  var piEstimate = 4*results.insideCircle/results.total;
  console.log("Estimated Pi to",piEstimate);
};

var numCores = require("os").cpus().length;
//create pool with maximum of numCores concurrent workers
var pool = require("workerrpc").pool(numCores, combineResults);

//add workload to pool and start working
pool.add(workloads);
//main.js END

Variant #2: Combine results using map/reduce

//main.js START
var numCores = require("os").cpus().length;

var workloads = require("../lib/workerrpc").createWorkload({
	maxParallel: numCores,
	script: "worker.js"
});

workloads.add(
	[{numOfTrials:1},{numOfTrials:2},{numOfTrials:4},{numOfTrials:8}]
);

workloads.
map(
	function doActualWork(workload,worker,next) {
		worker.pi(workload.numOfTrials,function(insideCircle) {
			next(null,{
				insideCircle:insideCircle,
				total:workload.numOfTrials
			});
		});
	}
).reduce(

	//WE ARE GOING TO SUM THE RESULTS UP, STARTING AT ZERO
	{insideCircle:0,total:0},

	//CODE TO ACTUALLY ADD RESULTS
	function sumSingleWorksResults(sum, current, next) {
		sum.insideCircle+=current.insideCircle;
		sum.total+=current.total;
		next(null,sum);
	},

	//FINISHED ADDING UP, PRINT RESULTS
	function printSumAndEstimate(err,sum) {
	  var piEstimate = 4*sum.insideCircle/sum.total;
	  console.log("Estimated Pi to be",piEstimate,"using",sum.total,"trials");
	}
);


//YOU CAN ADD WORKLOAD LATER ON
setTimeout(function() {
	workloads.add({numOfTrials:1000000});
},5000);
//main.js END

Download

The source is available for download from GitHub. Alternatively, you can install using Node Package Manager (npm):

npm install workerrpc

Test

Some basic tests are implemented using mocha.

mocha test -R spec

You can use npm test as well:

npm test