keyvank / pooljs

Browser computing unleashed!

Home Page:https://pooljs.ir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

alt text

Pool.js

What is Pool.js?

Pool.js is a JavaScript platform for Distibuted Browser Computing. The platform consists of the Pool.js Server which handles the connections and distributes the tasks between the Processors, which are the browsers connected to the Pool.js Server (And they may have multiple cores) in order to run distributed scripts and the Clients which run their scripts on the Processors.

For a browser to be a Processor, it should run the script located at: https://pooljs.ir/static/js/processor.js This script establishes a WebSocket connection to the Pool.js Server and fetches the SubProcesses and distributes them between the hosting machine's CPU cores.

A SubProcess is a single JavaScript function and its arguments, which returns part of the solution of a bigger problem.

One can add <script src="https://pooljs.ir/static/js/processor.js"></script> to his website so that every visitor of that website become a Pool.js Processor.

In order for the clients to run their scripts on the Pool.js Processors, they should run the script located at https://pooljs.ir/static/js/pool.js There are three library functions provided in this script for producing SubProcesses. All these functions accept a function which returns the result of the SubProcess as an object, as their first argument and they will return an object having a function named result() which accepts a callback and invokes it whenever a SubProcess has been done.

Consider the problem finding prime numbers between 1 to 1000, distributing the task between 10 browsers would be something like this:

Single process

for(var i = 0;i < 10; i++)
	pool.run(function(from,to){
		// Finding primes
	},[i*100,(i+1)*100]).result(function(primes){
		console.log(primes);
	});
}

pool.run(func,args /* Optional */); accepts a single SubProcess and posts it to the pool of Processors (You may also provide the arguments of this function as a list which in the example above two arguments specifying the range of numbers to search are provided)

Parallel for loop

pool.for(0,10,function(i){
	var from = i*100;
	var to = (i+1)*100;
	// Finding primes
}).result(function(primes){
	console.log(primes);
});

pool.for(from,to,func,extraArgs /* Optional */); is a parallel for loop which accepts a range and a function then creates multiple SubProcesses running in parallel for each index in the range. An index variable is provided to the function automatically as the index of the for loop. (You may also provide extra arguments for your function appearing after the index argument by setting the extraArgs argument to a list of objects)

Parallel for-each loop

pool.forEach([0,1,2,3,4,5,6,7,8,9],function(obj){
	var from = obj*100;
	var to = (obj+1)*100;
	// Finding primes
}).result(function(primes){
	console.log(primes);
});

pool.forEach(argsList,func,extraArgs /* Optional */); is a parallel for-each loop which accepts the list of object to be iterated and a function then creates multiple SubProcesses running in parallel for each object in the list provided. The first argument of the function provided is set to one of the objects in the list of objects provided. (You may also provide extra arguments for your function appearing after the first argument by setting the extraArgs argument to a list of objects)

Passing data

Notice that you can't use variables outside the scope of parallel function and you should pass them to the function as extra arguments like this:

var myVar1 = 2;
var myVar2 = [1,2,3];
pool.for(0,10,function(i,myVar1,myVar2){
	// Blah blah blah
}, [myVar1,myVar2]).result(function(){
	// Blah blah blah
});

Buffering

pool.setBufferSize(size); lets you set a buffer for the results of your SubProcesses in order to reduce the number of WebSocket messages. Pool.js Server will automatically flush the buffer when it is full. If the buffer is not yet full and you need the results you should call the pool.flush() function to manually flush the buffer.

pool.flush(); manually flush the result buffer in Pool.js Server.

Contribution

You can help this project by creating cool stuff on top of it!

Build status

Build Status

About

Browser computing unleashed!

https://pooljs.ir

License:Apache License 2.0


Languages

Language:CSS 28.6%Language:Python 26.4%Language:JavaScript 25.5%Language:HTML 19.5%Language:Makefile 0.0%